calling non-static nested class

Discussion in 'Java' started by Pradyut, Feb 5, 2007.

  1. Pradyut

    Pradyut Guest

    I have two classes under the package javaapplication9

    the first class: -
    package javaapplication9;

    /**
    *
    * @author Administrator
    */
    public class NewClass {

    /** Creates a new instance of NewClass */
    public NewClass() {
    }
    public int addn(int i)
    {
    return i + i;
    }
    }
    -----------------------------------------------------------------

    The second class: -
    package javaapplication9;

    /**
    *
    * @author Administrator
    */
    public class Main {

    /** Creates a new instance of Main */
    /*static Gear obj = new Gear();*/
    public Main() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("test");
    NewClass obj = new NewClass();
    System.out.println(obj.addn(8));
    Gear t =new Gear();
    t.Jam();
    }

    public /*static*/ class Gear
    {
    public void Jam()
    {
    System.out.println("Testing");
    }
    }
    }
    -----------------------------------------------------------------

    The problem:-
    I can call non-static class NewClass from main and not the non-static
    class Gear
    Why??
    Any other solution than declaring Gear static

    Thanks

    Pradyut
    http://pradyut.tk
    http://oop-edge.blogspot.com/
    http://pradyutb.blogspot.com/
    http://praddy-photos.blogspot.com/
    http://oop-edge.spaces.live.com/
    http://www.flickr.com/photos/praddy
    http://groups.google.com/group/oop_programming
    India
    Pradyut, Feb 5, 2007
    #1
    1. Advertising

  2. Pradyut

    Guest

    On Feb 5, 5:58 pm, "Pradyut" <> wrote:
    > I have two classes under the package javaapplication9
    >
    > the first class: -
    > package javaapplication9;
    >
    > /**
    > *
    > * @author Administrator
    > */
    > public class NewClass {
    >
    > /** Creates a new instance of NewClass */
    > public NewClass() {
    > }
    > public int addn(int i)
    > {
    > return i + i;
    > }}
    >
    > -----------------------------------------------------------------
    >
    > The second class: -
    > package javaapplication9;
    >
    > /**
    > *
    > * @author Administrator
    > */
    > public class Main {
    >
    > /** Creates a new instance of Main */
    > /*static Gear obj = new Gear();*/
    > public Main() {
    > }
    >
    > /**
    > * @param args the command line arguments
    > */
    > public static void main(String[] args) {
    > // TODO code application logic here
    > System.out.println("test");
    > NewClass obj = new NewClass();
    > System.out.println(obj.addn(8));
    > Gear t =new Gear();
    > t.Jam();
    > }
    >
    > public /*static*/ class Gear
    > {
    > public void Jam()
    > {
    > System.out.println("Testing");
    > }
    > }}
    >
    > -----------------------------------------------------------------
    >
    > The problem:-
    > I can call non-static class NewClass from main and not the non-static
    > class Gear
    > Why??
    > Any other solution than declaring Gear static
    >
    > Thanks
    >
    > Pradyuthttp://pradyut.tkhttp://oop-edge.blogspot.com/http://pradyutb.blogspot.com/http://praddy-photos.blogspot.com/http://oop-edge.spaces.live.com/http://www.flickr.com/photos/praddyhttp://groups.google.com/group/oop_programming
    > India


    try this:-
    Main.Gear gear = new Main.Gear();
    , Feb 5, 2007
    #2
    1. Advertising

  3. Pradyut

    Guest

    Correction is above post.
    Use this
    Main.Gear t =new Main().new Gear();
    , Feb 5, 2007
    #3
  4. Pradyut

    Lew Guest

    wrote:
    > Correction is above post.
    > Use this
    > Main.Gear t =new Main().new Gear();


    Explanation: Gear is an inner class, and needs an object of its outer class to
    give it nutrients. "new Main()" makes an object of the enclosing class, which
    object owns the non-static inner class, so from the object you call "new
    Gear()". Your original code did not have an enclosing object around the "new
    Gear()".

    Incidentally, the virtually universal and Sun-endorsed convention for Java
    nomenclature is to name classes with an initial upper-case letter, methods and
    non-static-final variables with an initial lower-case letter. The method
    "Jam()" would conventionally be named "jam()".

    - Lew
    Lew, Feb 6, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,708
    Thomas Weidenfeller
    Oct 12, 2005
  2. request@no_spam.com
    Replies:
    5
    Views:
    411
  3. Robin
    Replies:
    0
    Views:
    404
    Robin
    Jun 6, 2007
  4. Replies:
    2
    Views:
    470
  5. rickman
    Replies:
    5
    Views:
    413
    rickman
    Mar 30, 2013
Loading...

Share This Page