Results 1 to 8 of 8
Thread: nested classes
- 01-02-2010, 04:08 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 25
- Rep Power
- 0
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
-
- 01-02-2010, 04:24 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 25
- Rep Power
- 0
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
-
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.
- 01-02-2010, 04:37 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 25
- Rep Power
- 0
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
- 01-02-2010, 04:43 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-02-2010, 04:53 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 25
- Rep Power
- 0
this makes sense. Thanks. I have found my way to sun micors site to re-educate myself on this topic.
billq
- 01-02-2010, 05:16 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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")); } }
Jos
Similar Threads
-
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09-01-2009, 08:47 AM -
static vs. non-static nested classes
By rinke in forum Advanced JavaReplies: 8Last Post: 06-30-2009, 07:15 AM -
Nested Classes
By new.guy in forum New To JavaReplies: 13Last Post: 09-07-2008, 04:44 AM -
Nested(Inner) Classes
By Z.S.Tehrani in forum New To JavaReplies: 7Last Post: 08-13-2008, 10:54 AM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks