Inner classes in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    #1

    Inner classes in Java

    Hi,
    please any one say me..
    why we go for inner classes in Java??
    And importance behind that??
    please do needfull

    -Thanks,
    Hamsa
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by gaya3
    Hi,
    please any one say me..
    why we go for inner classes in Java??
    And importance behind that??
    please do needfull

    -Thanks,
    Hamsa
    And what does your Java textbook say?
    If it doesn't have a section for that, please throw it away.

    Here are some links to some good tutorials. Download Sun's tutorial. Its got a section for all that.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by gaya3
      Hi,
      please any one say me..
      why we go for inner classes in Java??
      And importance behind that??
      please do needfull

      -Thanks,
      Hamsa
      The importance of inner classes is, that they make life easier and safer. You could have separate classes in separate files for everything, but often you just want to use some class in one other class and that's when you use it. It can even be a private class then, as only the other classes in that file will have to see it. (Which makes it less vulnerable.)

      Greetings,
      Nepomuk

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nepomuk
        .. You could have separate classes in separate files for everything, but often you just want to use some class in one other class and that's when you use it. ...
        Remember that you are usually using some class in some other class even without having inner classes.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by r035198x
          Remember that you are usually using some class in some other class even without having inner classes.
          OK, what I meant was:
          "You could have separate classes in separate files for everything, but often you just want to use some class in only one other class and that's when you use it."

          Greetings,
          Nepomuk

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by nepomuk
            OK, what I meant was:
            "You could have separate classes in separate files for everything, but often you just want to use some class in only one other class and that's when you use it."

            Greetings,
            Nepomuk
            <Nitpicking again>
            Inner classes can be instantiated from other classes that are not their outer classes anyway.
            </Nitpicking again>

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Maybe the following example clarifies matters a bit:

              [code=java]
              public class Star {

              private String name;

              public Star(String name) { this.name= name; }

              public class Planet {

              private String name;

              public Planet(String name) { this.name= name; }

              public class Moon {

              private String name;

              public Moon(String name) { this.name= name; }

              public String toString() { return name+" (orbiting "+Planet.this+" )"; }
              }

              public String toString() { return name+" (orbiting "+Star.this+")" ; }
              }

              public String toString() { return name; }

              public static void main(String[] args) {

              System.out.prin tln(new Star("sun").new Planet("earth") .new Moon("moon"));
              }
              }
              [/code]

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                Maybe the following example clarifies matters a bit:

                [code=java]
                public class Star {

                private String name;

                public Star(String name) { this.name= name; }

                public class Planet {

                private String name;

                public Planet(String name) { this.name= name; }

                public class Moon {

                private String name;

                public Moon(String name) { this.name= name; }

                public String toString() { return name+" (orbiting "+Planet.this+" )"; }
                }

                public String toString() { return name+" (orbiting "+Star.this+")" ; }
                }

                public String toString() { return name; }

                public static void main(String[] args) {

                System.out.prin tln(new Star("sun").new Planet("earth") .new Moon("moon"));
                }
                }
                [/code]

                kind regards,

                Jos
                Thankfully not A T F E.

                Comment

                • kreagan
                  New Member
                  • Aug 2007
                  • 153

                  #9
                  Originally posted by r035198x
                  <Nitpicking again>
                  Inner classes can be instantiated from other classes that are not their outer classes anyway.
                  </Nitpicking again>
                  How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

                  [CODE=java]public Class1{
                  public InnerClass temp;

                  public Class1(){ temp = new InnerClass(); }
                  public InnerClass(){
                  public void someMethod(){}
                  }
                  }[/CODE]

                  [CODE=java]public Class2{
                  public void someMethodInCla ss2(){
                  InnerClass temp = new InnerClass(); //I'm sure this will give compiler error.
                  Class1 temp = new Class1();
                  temp.temp.someM ethod(); //this should work. This is how you access an inner class from outside that class.
                  }
                  }[/CODE]

                  I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

                  So how do you instantiate an object from an inner class?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by kreagan
                    How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

                    [CODE=java]public Class1{
                    public InnerClass temp;

                    public Class1(){ temp = new InnerClass(); }
                    public InnerClass(){
                    public void someMethod(){}
                    }
                    }[/CODE]

                    [CODE=java]public Class2{
                    public void someMethodInCla ss2(){
                    InnerClass temp = new InnerClass(); //I'm sure this will give compiler error.
                    Class1 temp = new Class1();
                    temp.temp.someM ethod(); //this should work. This is how you access an inner class from outside that class.
                    }
                    }[/CODE]

                    I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

                    So how do you instantiate an object from an inner class?
                    Have you read Jos' example above?

                    Comment

                    • kreagan
                      New Member
                      • Aug 2007
                      • 153

                      #11
                      Originally posted by r035198x
                      Have you read Jos' example above?
                      Lol... nope. Just did though.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by kreagan
                        How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

                        [CODE=java]public Class1{
                        public InnerClass temp;

                        public Class1(){ temp = new InnerClass(); }
                        public InnerClass(){
                        public void someMethod(){}
                        }
                        }[/CODE]

                        [CODE=java]public Class2{
                        public void someMethodInCla ss2(){
                        InnerClass temp = new InnerClass(); //I'm sure this will give compiler error.
                        Class1 temp = new Class1();
                        temp.temp.someM ethod(); //this should work. This is how you access an inner class from outside that class.
                        }
                        }[/CODE]

                        I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

                        So how do you instantiate an object from an inner class?
                        By having a reference available of the outer class; that's all there is to it. Read
                        my Star/Planet/Moon example again. If there is a star in scope you can create
                        a planet for it, if there is a planet in scope you can create a moon for it etc. etc.

                        kind regards,

                        Jos

                        Comment

                        Working...