Sample sample = new Sample();
sample.run();
sample.i=20;
sample.run();
sample.stop();
}
}
But im getting an error that "Illegeal modifier for the class Sample".
How to create a static class?
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
posted
0
Here is a quote from a JavaWorld article (linked here):
Top-level classes
You declare a top-level class at the top level as a member of a package. Each top-level class corresponds to its own java file sporting the same name as the class name.
A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
Inner classes might be used where only the containing class cares about their definition/implementation. Anonymous inner classes can be used for one-off implementations, used to create closures, and so on.
Vaishu Mol
Greenhorn
Joined: Jan 21, 2010
Posts: 5
posted
0
Here my sample innerclass code :
And here my top level class code goes :
My question is Which one is better code when compare with JVM?
Why we are coming to inner class even we can do the same program without using innerclass?
Please help me friends.
Ramakrishna Gutha
Greenhorn
Joined: Jan 18, 2010
Posts: 16
posted
0
Vaishu Mol wrote
What is the use of inner class?
Before addressing your question, first we should address below questions.
1. Is my class have more responsibilities (violating single responsibility principle)?
2. If yes, are the responsibilities private to my class ?
If the responsibilities are private to the class, then separate the extra responsibility from the outer class and put it in the nested/inner class.
Why and when to use nested/inner classes ?
1. Use nested/inner classes for better organization of the class, but don't overuse it.
2. If your class has an extra responsibility and if the scope is within the class, then go ahead and use nested/inner classes.
3. Writing a method adds a behavior to the outer class and writing a nested/inner class makes your outer class think about its own responsibility.
4. You gain all the advantages of OOP using inner classes where necessary.