Static Classes In Java

0 votes
Is there anything like static class in java?

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?
Dec 23, 2020 in Java by Rajiv
• 8,910 points
694 views

1 answer to this question.

0 votes

Java has static nested classes but it sounds like you're looking for 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 the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static class
  • 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
  • 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

Simple example per suggestions from above:

public class TestMyStaticClass {
     public static void main(String []args){
        MyStaticClass.setMyStaticMember(5);
        System.out.println("Static value: " + MyStaticClass.getMyStaticMember());
        System.out.println("Value squared: " + MyStaticClass.squareMyStaticMember());
        // MyStaticClass x = new MyStaticClass(); // results in compile time error
     }
}

// A top-level Java class mimicking static class behavior
public final class MyStaticClass {
    private MyStaticClass () { // private constructor
        myStaticMember = 1;
    }
    private static int myStaticMember;
    public static void setMyStaticMember(int val) {
        myStaticMember = val;
    }
    public static int getMyStaticMember() {
        return myStaticMember;
    }
    public static int squareMyStaticMember() {
        return myStaticMember * myStaticMember;
    }
}

Hope this helps!

Check out this Java Course to know more.

Thanks

answered Dec 23, 2020 by Gitika
• 65,910 points

edited Jul 5, 2023 by Khan Sarfaraz

Related Questions In Java

0 votes
2 answers

When to use Static Methods in Java?

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

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
15,528 views
0 votes
1 answer

Can Static methods be inherited in java?

A subclass inherits all of the public ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,120 points
1,639 views
0 votes
1 answer

Static Map initialization in Java

I would suggest you to use the ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,190 points
12,431 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
1,882 views
0 votes
2 answers

Why are you not able to declare a class as static in Java?

A static keyword  can be used with ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
1,107 views
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 9, 2018 in Java by anto.trigg4
• 3,440 points
1,540 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, 2019 in Java by Sirajul
• 59,230 points
1,532 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,910 points
115,073 views
+16 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
250,398 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP