top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why can't we declare a classes as static in java?

+1 vote
Why can't we declare a classes as static in java?
posted Dec 8, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

To prevent a particular class being instantiated you should add a private Constructor. This stops 'any other' Class from being able to create an object of type class.

In Java, the static keyword typically flags a method or field as existing not once per instance of a class, but once ever. A class exists once anyway so in effect, all classes are "static" in this way and all objects are instances of classes.

static does have a meaning for inner classes, which is entirely different: Usually an inner class instance can access the members of an outer class instance that it's tied to, but if the inner class is static, it does not have such a reference and can be instantiated without an instance of the outer class. Maybe you saw that someplace, then tried to use it on a top-level class, where it isn't meaningful.

answer Dec 24, 2015 by Amit Kumar Pandey
Similar Questions
+5 votes

In a static method, one can create only static variables, Then:

ClassA myclass = new ClassA();

does myclass is also an static variable?

+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?