Forums Register Login

Static Class

+Pie Number of slices to send: Send
Ranchers:

Consider this:



This code can successfully be compiled. Two questions from me are:
1. What would be the scenarios that we need to put an inner class into a class? What is the meaning actually having a class in a class? (I mean what is that InnerOne status actually inside the Class)
2. Amazingly, I could even can inlude a static keyword into the inner class declaration? But when I tried to include the static on the root class "One" , I got an error saying something like static modifier is not allowed here. I could understand why it couldn't be put on the root class, but why can we put the static keyword on the inner class? What is then the meaning with a static inner class.

Thanks guys,
Erik
+Pie Number of slices to send: Send
You might find this interesting: Getting in Touch with Your Inner Class.
+Pie Number of slices to send: Send
You cannot declare a static inner class. All inner classes are implicitly non-static. There are many times when you'd use one, but the most common one that I use is to permit the restriction of access scope of implementation operations. As an example, consider the following code:
ArrayList l = new ArrayList();
l.add(new Object());

There are a few problems with this code. One is that I can even declare an ArrayList reference in the first place (all references should be contract types). Second, that I can assign that reference - in this case, to a new ArrayList instance - finally, that I can invoke implementation operations (e.g. add) on the reference type. It would be better if all of these were not possible. This can be achieved with nested classes (which can be static by the way). In fact, as an experiment, consider the following:

Of all concrete reference types in ContractualJ API Specification, you can only declare them (the best case scenario within the confines of the language). You cannot assign them successfully (except to null) without a compile-time error (Type t = new Type() will not compile).

You cannot ever invoke any operations on these types successfully (since they are hidden) - this includes Object methods (best case scenario is immediate runtime failure, instead of the more optimal compile-time failure).

This has all been achieved by using nested classes - among other things.

What is the difference between a nested class, an inner class, a local class, an anonymous class and a member class (or interface)?
http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true

ContractualJ API Specification
http://contractualj.com/api
+Pie Number of slices to send: Send
 

Originally posted by Erik Dirgaria:
Ranchers:
1. What would be the scenarios that we need to put an inner class into a class?



Simple answer would be if a class is to be used in only one class then put it in that class only.

This example will help you clear your concepts:

Iterator is an interface in Collection framework, used to iterate over different Collections Classes e.g. ArrayList, Vector, HashSet, HashMap etc.
iterator() method in each these classes returns an object of class implementing Iterator. Now since all these classes have different underlying datastructures, so implementation of Iterator has to be different for each of these class. So instead of providing one Iterator implementation class for each of these classes(e.g ArrayListIterator, VectorIterator etc.) SUN decided to provide Iterator implimentation classes as inner class in respective collection class. So ArrayList, Vector etc. each has one innerclass implementing Iterator.

Moreover user will never need to make an instance of Iterator implementing class via new. So it makes sense to make in inner.

Regards,
Jass
[ April 28, 2006: Message edited by: Jass Singh ]
+Pie Number of slices to send: Send
 

You cannot declare a static inner class



I think you can declare static inner class

public class A{

private static int a=10;

public static class B{
static int x;
B(){x=a;System.out.println(x);}


}

}
class Test{
p.s.v.m.(String args[]){
A.B b=new A.B();//x=10 after execution of this line
}
}
+Pie Number of slices to send: Send
 

Originally posted by Tony Morris:
You cannot declare a static inner class. All inner classes are implicitly non-static...


That's true, and it illustrates a serious point of confusion. According to the current Java Language Specification...

"A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class." (Ref: 8 Classes.)

"An inner class is a nested class that is not explicitly or implicitly declared static." (Ref: 8.1.3 Inner Classes and Enclosing Instances.)

But unfortunately, these terms have not always been so well defined. In particular, the term "inner class" was frequently used in place of "nested class," and "nested class" was used to describe a "static inner class" -- which is a distinct contradiction under today's terminology. Even more unfortunate is that the old terminology is still prevalent (for example, in Eckel's new 4th edition of Thinking in Java).
[ April 28, 2006: Message edited by: marc weber ]
+Pie Number of slices to send: Send
 

Originally posted by Shrinivas Mujumdar:
I think you can declare static inner class



That's a nested class, not an inner class.
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1152 times.
Similar Threads
Can inner classes be private?
Inheritance from inner Class
inner class questoin
Sun Test Question !!!!!!!!!!!
Sun's sample quest #2
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:59:27.