answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why a static member method can access only static members in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


What is the difference between static and nonstatic member with example in java?

It depends on whether the member is a static variable or a static method of the class.A non-static member variable is an instance variable. That is, each instance of the class has its own independent set of instance variables.A static member variable is not associated with any one instance of the class, and exists even when there are no instances of the class. As with all static variables, it exists for the entire duration of the program.A non-static member method is an instance method, thus the method automatically inherits a this pointer.A static member method does not inherit a this pointer, but it does have private access to to the class. Thus specific instances can be passed to a static method if necessary.Static members can be thought of as being common to all instances of a class, rather than a specific instance, even though no instances are actually required in order to make use of them.


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.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.

Related questions

What is a static data linkage?

Static data members of a class in namespace scope have external linkage. Static data members follow the usual class access rules, except that they can be initialized in file scope. Static data members and their initializers can access other static private and protected members of their class. The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile. The declaration of a static data member in the member list of a class is not a definition. The definition of a static data member is equivalent to an external variable definition. You must define the static member outside of the class declaration in namespace scope.


Why is it not advisable to use this pointer with static members?

It is not inadvisable, it is impossible. Static member methods do not have access to a this pointer since they are not associated with any instance. Static members are scoped to the class, not to an object (an instance of the class). Only instance members have access to the this pointer.


How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


What is the difference between static and nonstatic member with example in java?

It depends on whether the member is a static variable or a static method of the class.A non-static member variable is an instance variable. That is, each instance of the class has its own independent set of instance variables.A static member variable is not associated with any one instance of the class, and exists even when there are no instances of the class. As with all static variables, it exists for the entire duration of the program.A non-static member method is an instance method, thus the method automatically inherits a this pointer.A static member method does not inherit a this pointer, but it does have private access to to the class. Thus specific instances can be passed to a static method if necessary.Static members can be thought of as being common to all instances of a class, rather than a specific instance, even though no instances are actually required in order to make use of them.


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.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7


Tell about static method?

in java a method is said to be static if 'static 'keyword is used before the method name . foe ex.- static void show(){ ........ } this method has the following property-- 1. it can invoke only a static method. 2. it can't be reffered using keyword 'this','super'. 3.static method can access only a STATIC MEMBER VARIABLE or STATIC CLASS VARIABLE . 4. there should not be static & non static version of a nethod in a class . 5.static method can be used before the creation of d object of dt class.


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.


Why you have not access to non-static members of the enclosing class?

Non-static members are only accessible by instantiating an object of the class. However, access to those members is determined by the access specifier applied to each member. Private members are visible to all instances of the class (including other instances) and to friends of the class. Protected members are the same as private members but are also visible to derived classes. Public members are unrestricted.


What is need of static method?

If you need to access a method without creating an object of corresponding class, it need to be a static method.


Difference between static and non static constructor?

A static constructor is used to do anything you need done before any static methods are called such as static variable initialization. In Java (as in C#) when a static constructor is called is non-deterministic but will always be called before a static method on the same class.


Why static is used in main method?

It is declared static because the JVM would need to execute the main method and if it is not static the JVM would need an object of the class to access the method. How can the JVM get an object of a class without invoking it. since it is static, the JVM can easily access it without this need to have an object of the class.