0

Experienced product developers,web developers and gurus in this forum ,kindly share your experiences.

0

3 Answers 3

3

static class : top level class can't be declared as static. Only Member and Nested top-level classes can be defined as static.

You declare member classes when you want to use variables and methods of the containing class without explicit delegation. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal top-level class as explained above.

nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package.

static methods : mostly for utility to be shared across instances/application. A good example of this are the many utility methods in the predefined Math class

static variables : information to be shared amongst instances. Mostly used as constants.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks then when to use static final and static variables
@Suresh: When you want to share it across instances.
1

The Java Tutorial: Understanding Instance and Class Members

Comments

0

static variables: when ever you need to have a variable in your class for all instances. this variable will be shared between instances.

static class: i usually try to avoid static classes and use singleton instead. (inner classes are exception: if you don't need to access to outer class variables its better to make your inner class static)

1 Comment

you cannot declare a top level class as static in Java.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.