File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to create a static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create a static class " Watch "How to create a static class " New topic
Author

How to create a static class

Vaishu Mol
Greenhorn

Joined: Jan 21, 2010
Posts: 5
Hi friends,
Im new to java codes. Im learning from the basic things.

How to create a static class. My coding goes like this :

package com.learning.sampleclass;

public static class Sample {
int i = 10;
String name = "Class";
void run(){
System.out.println("Integer : " + i);
}
void stop() {
System.out.println("Application closed......");
}

}
public class HelloClass {

/**
* @param args
*/
public static void main(String[] args) {

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
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."
Vaishu Mol
Greenhorn

Joined: Jan 21, 2010
Posts: 5
Thanks a lot buddy
Vaishu Mol
Greenhorn

Joined: Jan 21, 2010
Posts: 5
And im coming with another doubt :-)

What is the use of inner class?


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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

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.

Fallow the link for more info http://onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index2.html

I hope this is cleared.

Regards
GRK
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Those are quote tags, not code tags.
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
One should be really careful when posting the content, improper content may lead to be confusing among new folks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
David Newton wrote:Those are quote tags, not code tags.
Agree. Changed them, and got rid of the coloured and large text.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Ramakrishna Gutha wrote:One should be really careful when posting the content, improper content may lead to be confusing among new folks.
Agree. I can see some serious errors in your code, I am afraid.
Ramakrishna Gutha
Greenhorn

Joined: Jan 18, 2010
Posts: 16
Campbell,

I haven't posted any code.

Thanks
GRK
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I meant the code you had commented about. Very sorry for the confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a static class
 
Similar Threads
Doubt in Thread Ques
Doubt in Overloading
Inner classes
Multiple inheritance Topic, need answer!!!!!!!
main method