Results 1 to 8 of 8

Thread: nested classes

  1. #1
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default nested classes

    I am confused on the creation of a nested class.
    public class someClass
    {
    ....
    public static class NestedClass
    {
    ....
    }
    }
    To create the nestedClass object the tutorial use the new command.
    NestedClass x = new NestedClass();

    I thought that if something was labels as static you did not have to create the object using new?
    thanks
    bill

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

    Default

    Quote Originally Posted by billq View Post
    I thought that if something was labels as static you did not have to create the object using new?
    Where have you read that?

  3. #3
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    In the tutorial
    math.hws.edu/javanotes/c6/s1.html http: goes before math. I only have 6 posts and I need 20 in order to post a link.
    the author created a HelloWorldDisplay static nested class inside main.
    This really confuses me.
    thanks
    bill

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

    Default

    No, you misunderstand me. Where did you read this?

    I thought that if something was labels as static you did not have to create the object using new?

    It's blatantly wrong.

  5. #5
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    What is blatantly wrong. Me or the author? In order to use the Math class I do not have to create an object of Math to get Math.sqrt(). I thought static things were created at compile time and not at runtime. Am I totally confused about this?
    thanks
    bill

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by billq View Post
    What is blatantly wrong. Me or the author? In order to use the Math class I do not have to create an object of Math to get Math.sqrt(). I thought static things were created at compile time and not at runtime. Am I totally confused about this?
    A static class is just like any other (static) class member. You can instantiate a member of that class (using the 'new' operator) when that class is visible from the context where you want to instantiate it. You can me private static classses (can only be instantiated when you are in the context of the outer class), protected static classes (same as the first but in the context of subclasses of the outer class it can also be instantiated), package scope or public classes.

    A non static class only exists when an object (instantiation) of the outer enclosing class exists. Those classes are more interesting.

    kind regards,

    Jos

  7. #7
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default

    this makes sense. Thanks. I have found my way to sun micors site to re-educate myself on this topic.
    billq

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by billq View Post
    this makes sense. Thanks. I have found my way to sun micors site to re-educate myself on this topic.
    About those non-static nested classes; the following example can create solar systems where a star is an instantiation of the outer class; a planet is an instantiation of a nested class (a star has to exist in order to create a planet) and just for fun the planet class also has a nested class that represents a moon of a planet circling a star:

    Java Code:
    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.println(new Star("sun").new Planet("earth").new Moon("moon"));
    	}
    }
    kind regards,

    Jos

Similar Threads

  1. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  2. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM
  3. Nested Classes
    By new.guy in forum New To Java
    Replies: 13
    Last Post: 09-07-2008, 04:44 AM
  4. Nested(Inner) Classes
    By Z.S.Tehrani in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 10:54 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 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
  •