answersLogoWhite

0

Why toplevel class cannot be static?

Updated: 8/18/2019
User Avatar

Wiki User

13y ago

Best Answer

A toplevel class certainly can be static. Static has nothing to do with the level of a class.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why toplevel class cannot be static?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why toplevel class cannot be private?

it can access in entire application.


What happens when you declare class as a static in Java?

Declaring an inner class static means that class only has access to the "outer" class public and private static fields. A non-static inner class has access to the outer class's instance data. Top-level classes cannot be declared static. The advantage of a static inner class is that it doesn't need an instance of the containing class to work and it's bytecode class size is smaller for that reason - less overhead.


What is the type of class for which object cannot be created?

A class that is declared as "final" cannot be inherited.


What is static class?

A static class is a class where all the members are declared static.


What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


Why static functions cannot use instant variables?

Static functions are tied to a class, not to a particular object. A static function can only access static variables because it has no knowledge of member variables.


It is perfectly legal to any instance variable inside of a static method?

No. You will get compilation errors. The complier will complain that you are trying to access non static variables from inside a static method. A static method can access only static variables.


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them


Why can't declare construtor as a static final abstract?

Because of the following reasons:static - If a constructor is static, an object instance cannot invoke it to initialize itself (Because static members are not linked to an object)abstract - because an abstract class cannot be instantiated and hence it will not have a constructor. If you make a concrete class's constructor abstract - it cannot be instantiated. eitherways it makes no sensefinal - a constructor cannot be final (thats the way java is designed)


Are the methods or members of static class static?

A static class may or may not have static members. Adding the static keyword to the class definition won't change this. Note that an inner class which is not static may not contain static members (unless those members are also declared final).


How do you declare a member of a class static?

Static MethodsStatic keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class)Ex:public class StaticTest {public static String getAuthorName() {return "Anand";}}Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as:String authorName = StaticTest.getAuthorName();Tip: It is important to know that, a static method cannot and I mean CANNOT access instance variables or methods of the class. It can only access other static methods and variables.For Ex:public class StaticTestBad {private String name = "Rocky";public static String getAuthorName () {return name; //Cant work!!!}public int getAge(){return 28;}public static int getAuthorAge() {return getAge(); //Cant Work Again!!!}}If you try to compile this class, you will get compilation errors at the two lines that I have commented out.Static VariablesThe static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


How is dynamic initialization of objects is achieved?

by creating class as an static class