Results 1 to 10 of 10
Thread: java inner class question
- 02-14-2009, 04:37 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
java inner class question
Hello. I am completely new to java. I have C/C++ background. I am quite puzzled at java inner class.
Given the following code:
class Outer
{
private string _name = "outer";
private int _data = 123;
public Outer
{
// It seems these two methods produce same results.
Inner t = new Inner();
Inner t2 = this.new Inner();
t.test();
t2.test();
}
class Inner
{
private string _name = "inner";
public void test()
{
// How to access Outer's _name?
System.out.println(_name);
}
}
}
I have two questions:
1. According to all tutorials, I have use this.new to create an instance of Inner class, but new can do the same. What's the difference?
2. How can Inner's test method access Outer class' _name member variable?
-
this.new is redundant here and is not necessary. You must call a non-static inner class "on an instance" of the outer class, but that instance is implicitly present within any of outer's methods, so this isn't needed.
For number 2, an inner class should have access to all of the outer's fields if I'm remembering correctly, so there should be no problem with inner accessing the _name field. Is it throwing a compile time error?
- 02-14-2009, 04:58 AM #3
Use <CODE> code here </CODE> (replace < and > with [ and ]) tags, makes class much more easy to read.
As someone already said, all non-static methods automatically put the "this." when other non-static methods are called(Or constructors).Java Code:class Outer { private [COLOR="Red"]string[/COLOR] [COLOR="blue"]_name[/COLOR] = "outer"; private int _data = 123; public Outer { // It seems these two methods produce same results. Inner t = new Inner(); Inner t2 = this.new Inner(); t.test(); t2.test(); } class Inner { private [COLOR="red"]string [/COLOR][COLOR="Blue"]_name[/COLOR] = "inner"; public void test() { // How to access Outer's _name? System.out.println(_name); } } }
1. it's String, not string.
2. You could refer to the outer classes _name field, but you can't in this situation because your inner class has another field with the exact same name, which shadows your outer class _name field, in other words when you refer to _name, it assumes you are talking about the inner class's field, not outer class because it is farther away. Try changing the inner class's _name field's name, maybe to innername or something.
3. _name. It is bad naming convention to use an underscore to start a variable name. Just a tip.
Hope this helped.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-14-2009, 05:04 AM #4
Inner vs Outer
Well, as posted the code won't compile. the Outer class constructor needs pararentesis and string types need a capital "S". Besides that, it works fine or as expected.
I was going to answer the questions as follows (with my limited knowledge):
1. Never seen an intantiation of a class using "this", but I guess it could be (Fubarable's explanation sounds better).
2. Because the inner class is within the scope of the outer class, therefore the the inner class has access to the Outer class variables.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 05:05 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
Question #2 is solved.
I am still puzzled at #1.
Is new a keyword? What does this.new mean? Or this.new is only a 'contextual' keyword that is only relevant to inner classes?
- 02-14-2009, 05:09 AM #6
this and that
Last edited by CJSLMAN; 02-14-2009 at 05:17 AM.
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 05:38 AM #7
"this" means the current object, i.e. if you use "this" in a non static method, it refers to the object that called the method. So if you call another non-static method within a non-static method, you could write "this.otherNonStaticMethodName();"
for example, but the "this" is unecessary, it is automatically assumed, so you can just write "otherNonStaticMethodName();". It's the same in your case except that you're creating an object, not calling a method. A static method cannot do this obviously because it has no object that calls it. However, a static method can create an object and use it to call non-static methods. Static methods can be called from within a class like this "staticMethod();", but if it is called from another class, it must be "StaticMethodsClass.staticMethod();". The this keyword can also call a constructor, for example:
Hope this isn't too confusing, hope this helps.Java Code:public class Example { private int x; public Example(int x) { this.x = x; // in this case, this refers to the field, not the variable // this is only needed because the variable shadows the field, //redundant in other cases. } public Example() { this(5); // calls the other constructor } }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-14-2009, 05:57 AM #8
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
In cases like this, if you really need to resolve the ambiguity, then you can write e.g.:
Java Code:Outer.this.name
Neil Coffey
Javamex - Java tutorials and performance info
- 02-14-2009, 06:56 AM #9
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The difference is scoping. If you are creating the inner class from within the outer class, then you do not have use the bizzaro new A. new B syntax, you just use 'new B'.
c++ has inner classes as well, so the concept should be familiar, even if the specifics are not.
- 02-15-2009, 05:08 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
I have some question about jar and class??
By low224 in forum New To JavaReplies: 2Last Post: 01-01-2009, 04:02 AM -
Abstract Class question
By maa11235 in forum New To JavaReplies: 1Last Post: 01-05-2008, 10:30 PM -
class organization question
By maa11235 in forum New To JavaReplies: 0Last Post: 12-17-2007, 03:25 AM -
question regarding class
By kavithas in forum New To JavaReplies: 4Last Post: 11-16-2007, 09:12 AM -
question about Class.forName()
By oregon in forum JDBCReplies: 4Last Post: 08-01-2007, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks