Does Java allow to create static classes?

0 votes
If it is possible - what is the meaning of such a class. Do all the methods of the static class need to be static too?

Is it required the other way round, that if a class contains all the static methods, shall the class be static too?

What are static classes good for?
May 17, 2018 in Java by parth295
• 4,620 points
54 views

2 answers to this question.

0 votes

n votceJava has static nested classes but you need a top-level static class. Java has no way of making a top-level class static but you can simulate a static class like this:

  • Declare your class final - Prevents extension of the class since extending a static class makes no sense
  • Make all the members and functions of the class static - Since the class cannot be instantiated no instance methods can be called or instance fields accessed
  • Make the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static class

Note that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance member

down voteaccepted

answered May 17, 2018 by sharth
• 3,320 points
0 votes
Java has "static nested classes", but they are not at all the same as C#'s static classes, if that's where you were coming from.

A static nested class is just one which doesn't implicitly have a reference to an instance of the outer class.

Static nested classes can have instance methods and static methods.

There's no such thing as a top-level static class in Java.
answered Sep 5, 2018 by Sushmita
• 6,880 points

Related Questions In Java

0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,050 points
42 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
98 views
0 votes
1 answer

Is it possible to create a memory leak with Java

Here's a good way to create a memory ...READ MORE

answered May 30, 2018 in Java by parth295
• 4,620 points
221 views
0 votes
1 answer

Does python have an equivalent to Java Class.forName()?

This is found in the python standard ...READ MORE

answered Jun 1, 2018 in Java by parth295
• 4,620 points
183 views
0 votes
1 answer

Non-static method within Static method in Java

As per my knowledge, you are getting this error ...READ MORE

answered Sep 27, 2018 in Java by anto.trigg4
• 3,420 points
105 views
0 votes
2 answers
0 votes
1 answer

Can't access static fields in inner classes in Java

Well, the major idea behind this concept ...READ MORE

answered Oct 8, 2018 in Java by anto.trigg4
• 3,420 points
69 views
0 votes
2 answers

Why it is not possible to define a static method in a Java interface?

Interfaces are concerned with polymorphism which is ...READ MORE

answered Aug 27 in Java by Sirajul
• 25,700 points
68 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 25,700 points
134 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
79 views