Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By deepali

Thread: Doubts about nested class

  1. #1
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Doubts about nested class

    Hi ALl,

    I was learning interface [as a constant tank] and made demo file, in which I need to use Nested class.
    Here is the code

    Interface Code
    Java Code:
    interface Constant1
    {
    double pi=3.14;
    }

    Class code
    Java Code:
    class CricleArea implements Constant1
    {
    static class Circle
    {
    int r;
    Circle(int r)
    {this.r=r;}
    
    double area()
    {return pi*r*r;}
    }//end.nestedClass
    
    
    public static void main(String[] args)
    {
    Circle c = new Circle(5);
    
    System.out.println("Area is :"+c.area());
    }//end.main
    }//end.class
    This don't work as expected unless I declare the nested class circle as static.
    As main method is static it can't refer a non static >> new Circle(5) [this what error message say]


    My doubts is,

    in case of nested calls only main method gives this kind of error, else it process all other calsses(non-nested one) directly without letting us declare them static.


    Why so ??

    Why a nested class in the above case needs to be declared specifically static ?

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,078
    Rep Power
    6

    Default Re: Doubts about nested class

    Because you're trying to use it from a static method. See it as that your main() is not actually 'part of' your CricleArea (watch those typos) class; if you declare Circle as a non-static inner class, it IS part of your CricleArea class.

    As an exercise, try making Circle non-static and then creating an instance of it in a (non-static) method of CricleArea; you'll see that it then works.
    John2find likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    deepali is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Doubts about nested class

    John2find likes this.

  4. #4
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Doubts about nested class

    Quote Originally Posted by gimbal2 View Post
    Because you're trying to use it from a static method. See it as that your main() is not actually 'part of' your CricleArea (watch those typos) class; if you declare Circle as a non-static inner class, it IS part of your CricleArea class.

    As an exercise, try making Circle non-static and then creating an instance of it in a (non-static) method of CricleArea; you'll see that it then works.

    Thank you

Similar Threads

  1. nested class/method
    By Russd772 in forum New To Java
    Replies: 7
    Last Post: 01-08-2013, 09:50 PM
  2. inner vs nested class
    By sai_1245 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 05:11 AM
  3. Adding custom Jtable to GUI in nested class
    By smallmos1 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 12:55 PM
  4. Use a 'nested class' here?
    By jezza10181 in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 01:15 PM
  5. Nested List class for Simulation Queue
    By Cheddz in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 01:19 AM

Posting Permissions

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