• Post Reply Bookmark Topic Watch Topic
  • New Topic

use of static nested class?  RSS feed

 
nitinthe man sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai everybody...
I am not getting the situation where use of static nested class is must or better.could you tell me where should i use static nested class.

thank you very much.
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heyas
>>I am not getting the situation where use of static nested class is must or better.could you tell me where should i use static nested class.
well, ill quickly define it for you to give you an indication of what excactly we are talking about. Firstly an inner class is a class within another class
public class Example {
...
class InnerClassExample
{}
}
"As a member of its enclosing class, a nested class has a special privilege: It has unlimited access to its enclosing class's members, even if they are declared private." -java.sun.com
basically you will create an inner class when a class's relevance is totally dependant upon another class. For example, if you only have pages in the context of a book, then class page could be an inner class of Book.
as with all other members, an inner class can be defined as static or not. static means that there is NO instance created of a class. For example

public class GetAge{
public static int age;
}
will create one variable called age, reguardless of the number of instances created of GetAge. whereas 'public int age;' would create a new variable, age, for every instance of that class.
Going back to 'inner classes'. any class defined within another class is called an inner class; unless the 'inner' class is defined as static, in which case it is called a 'nested class'.
so an inner class will have an instance created with every instance made of its outer class, whereas a nested class will have one exististance regardless of how many instances are being created from its outer class. a static nested class cannot refer directly to instance variables or methods defined in its enclosing class-it can use them only through an object reference.
for more information please see:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
-twans
 
nitinthe man sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai Heyas,
Thanks, you spent time for me.
when i have a class "A" which need to access private members of class "B" because its depend on class "B" ,I should make "A" as the inner class of "B".
am i right. or something different?
thanks.
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again
yes, that is correct. some things only exist or make 'sense' in the context of another thing.
this means that A can only exist relating to B. Therefore B can have private attributes which A can access.
For example:
A stearing wheel in a car, only makes sense in the context of a car. You dont often have a stearing wheel by itself. If the stearing wheel needs to know a private attribute about the car, such as serial number, so as to match its serial number with that of the car then the following occurs:
public class Car {
private String serialNumber = "q93Op5G";
public class StearingWheel {
private String wheelSerial = serialNumber;
}
}
note: we have created an 'inner' class called StearingWheel. This is due to the fact that a stearing wheel (for the purposes of the example)only exists when a car is present. A new instance of this Stearing Wheel class is created when every instance of class Car is created; as every car has its OWN stearing wheel.
We would want a 'nested class' or static inner class, however, if every car SHARED the SAME stearing wheel. which obviously is not the case.
i hope this answers your quieries somewhat
-twans
 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
i just wanted to ask about the use of public access modifier for the inner class, althought unless a method-local inner class , all others inner classes are thought as menber variable so they can have public as a modifier but somewhere i read there can only be one public class per source file,
should i assume it does not apply to inner classes?
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>all others inner classes are thought as menber variable so they can have public as a modifier but somewhere i read there can only be one public class per source file,
should i assume it does not apply to inner classes?
that is an excellent question. The reason being for the limitations on public class's in a file is because the standard is the following:
public class Example
=> this will save and compile as Example.class
if we have public class Example, public class Example2
which file should be held as the .class file? this conflict prevents java from allowing multiple public classes to be in the same source, UNLESS they are inner classes
when you copile an inner class, for example:
- public class OuterClassExample
- public class InnerClassExample
if we compiled this, we would hava OuterClassExample.class, and OuterClassExample$InnerClassExample.class
java uses the '$' symbol to differentiate between what is a public inner class, and just an illegal statement, of multiple public classes.
the following is my initial example which compiles fine and has been modified to have multiple inner classes. Please note how i have declared the instances of the classes in my main method, it is somewhat different to normal initialization. Also, if i wanted to avoid these declorations, i would create the instances of the inner classes in the constructor of my outer class

hope that helps somewhat
-twans
 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it did help!
thanks
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem.
glad to help
twans
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!