answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

No. you cannot access or use instance variables inside static methods. 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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: It is perfectly legal to any instance variable inside of a static method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A variable that is declared inside a method is called variable?

It is called a local variable since it only exists inside the method.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


What is it called when java allows you to declare methods with the same name in a class?

Yes perfectly Legal Reason: The scope of a variable declared inside a method is only till the method's end. So by defining another variable of the same name inside a different method does not create any issues.


What does a Java Boolean type variable declared but not yet initialized contain when its class is instanciated?

If it is a class or instance variable, it gets the default value of false. If it is a local variable (inside a method), it doesn't have a value until you explicitly set one.


What are the 6 ways to use this keyword?

"Java This keyword" is a reference to the current object, it is very helpful when you need to refer an instance of a particular Object from its available methods or using it's constructor, also "this" keyword helps us to avoid naming conflicts.The following are different ways to use java this keyword1) Using with instance variable2) Using with Constructor3) Pass / Return current instanceUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with ConstructorUsing this keyword inside constructor like followingthis("Sony", 20); it will call the constructor having same parameter

Related questions

What is the difference between variable declared inside the declaration part and variable declared in scriptlet part?

Variable declared inside declaration part is treated as a global variable, which means after translation of jsp file into servletthat variable will be declared outside the service method as an instance variablethe scope is available to the complete jspVariable declared inside a scriplet will be declared inside a service method as a local variable and the scope is with in the service method.


A variable that is declared inside a method is called variable?

It is called a local variable since it only exists inside the method.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


What is it called when java allows you to declare methods with the same name in a class?

Yes perfectly Legal Reason: The scope of a variable declared inside a method is only till the method's end. So by defining another variable of the same name inside a different method does not create any issues.


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().


What does a Java Boolean type variable declared but not yet initialized contain when its class is instanciated?

If it is a class or instance variable, it gets the default value of false. If it is a local variable (inside a method), it doesn't have a value until you explicitly set one.


What are the 6 ways to use this keyword?

"Java This keyword" is a reference to the current object, it is very helpful when you need to refer an instance of a particular Object from its available methods or using it's constructor, also "this" keyword helps us to avoid naming conflicts.The following are different ways to use java this keyword1) Using with instance variable2) Using with Constructor3) Pass / Return current instanceUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with instance variableUsing this keyword inside a method or constructor it will use instance variable instead of local variable, in the absence of this keyword it will use local variableUsing with ConstructorUsing this keyword inside constructor like followingthis("Sony", 20); it will call the constructor having same parameter


Can static variable be used inside a static method?

Yes.


Can you declears static in a local variable in a method in java?

we cannot use the staic keyword inside the method... But we can use the final keyword inside the method....


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


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.


what is the difference between final and static keywords?

static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...