Results 1 to 10 of 10
  1. #1
    neo_in_matrix is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Question 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Use <CODE> code here </CODE> (replace < and > with [ and ]) tags, makes class much more easy to read.
    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);
              }
         }
    }
    As someone already said, all non-static methods automatically put the "this." when other non-static methods are called(Or constructors).
    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.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    neo_in_matrix is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    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?

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default this and that

    Last edited by CJSLMAN; 02-14-2009 at 06:17 AM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    "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:
    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
         }
    }
    Hope this isn't too confusing, hope this helps.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    In cases like this, if you really need to resolve the ambiguity, then you can write e.g.:

    Java Code:
    Outer.this.name

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by neo_in_matrix View Post
    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?
    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.

  10. #10
    neo_in_matrix is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. I have some question about jar and class??
    By low224 in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 05:02 AM
  2. Abstract Class question
    By maa11235 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 11:30 PM
  3. class organization question
    By maa11235 in forum New To Java
    Replies: 0
    Last Post: 12-17-2007, 04:25 AM
  4. question regarding class
    By kavithas in forum New To Java
    Replies: 4
    Last Post: 11-16-2007, 10:12 AM
  5. question about Class.forName()
    By oregon in forum JDBC
    Replies: 4
    Last Post: 08-01-2007, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •