Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a static class

 
Vaishu Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Vaishu Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot buddy
 
Vaishu Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And im coming with another doubt :-)

What is the use of inner class?


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are quote tags, not code tags.
 
Ramakrishna Gutha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One should be really careful when posting the content, improper content may lead to be confusing among new folks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,

I haven't posted any code.

Thanks
GRK
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant the code you had commented about. Very sorry for the confusion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic