Can a non-static method be accessed from a static method?
And can a static method be accessed from a non-static method?
A static method can be accessed from a non-static, but not the other way around. If you're inside a static method, you can only access a non-static method if you have an instance of an object on which to call the method.
Can a non-static method be accessed from a static method?
No because an object is required to access non-static method and there are chances that an object is not available while accessing a static method
can a static method be accessed from a non-static method?
Yes.
To access a non-static method, you need to have an instance of an object. If you are inside a static method, then you are not able to call non-static method, because, per definition, you are not "inside" an object (so you don't have any object to call this method on). You can however call non-static methods from static if you have a reference to the object you want to call the non-static method on.
Related
I have a question that I can't answer
which one is NOT true!
satic methods can access to static variable and static methods
static methods can access to non-static variable and non-static methods
non-static methods can access to static variable
non-static methods can access to static methods
static methods can access to static variable and static methods :- true
Reason :-class level fields/methods can access the other class level fields/methods
static methods can access to non-static variable and non-static method :- false
Reason :-class level fields/methods can not access the object level fields/methods as there is a probability object has not been created yet
non-static methods can access to static variable :- true
Reason :-Object level fields/methods can access the class level fields/methods as its logical
non-static methods can access to static methods :- true
Reason :-Object level fields/methods can access the class level fields/methods as its logical
2nd is partially wrong.
Static method can't directly access non static method. But, they can access non static methods on an object corresponding to the methods.
2 is FALSE Because at the time you call a static method it is not guaranteed, that the instance (non-static) variables/functions exist.
No 2 is not True.
Static methods can only access static members and variables.
Non-static methods can access both static and non-static members and variables.
So I am calling a method defined in the main method of the same class. I am referencing the callable method using 'this' keyword. Why cannot I call this non-static method, as shown below, in static main method?
Relevant but a little different question, by choice of design, would it make sense to construct a separate class for implementing logic. Essentially, main class should only have a main method to keep things simple. Any ideas will be appreciated.
UniqueChars.java:8: non-static variable this cannot be referenced from a static context
System.out.println(this.hasAllUniqueChars(input));
^
1 error
public class UniqueChars {
boolean hasAllUniqueChars(String input) {
return false;
}
public static void main(String[] args) {
String input = "Harry";
System.out.println(this.hasAllUniqueChars(input));
}
}
Because this is non-static. this is an instance, and you cannot reference instance variables within a static method since static means "one for the entire class."
You should make the hasAllUniqueChars method static (like static boolean hasAllUniqueChars(String input)), so you could do
System.out.println(UniqueChars.hasAllUniqueChars(input));
Or just (since you're already in the class)
System.out.println(hasAllUniqueChars(input));
You could also keep it non-static, and do this:
UniqueChars uc = new UniqueChars();
System.out.println(uc.hasAllUniqueChars(input));
You can only use "this" within an instance of an object.
So you either need to make a UniqueChars object, or you need to make hasAllUniqueChars static
Because there's no this in a static method, because this refers to the current instance (e.g., an instantiated object of that class).
There's no instance for static methods: that's why they're called static methods. They live at the class level, and are not associated with any instance.
You should either (a) create an instance and call the instance method on it, or (b) make the method static and don't bother. If the class doesn't need any instance-specific state, might as well make it static, e.g., a utility method.
Use this code:
System.out.println(new UniqueChars().hasAllUniqueChars(input));
Method hasAllUniqueChars is not static. To make it static you have to put static before name boolean like static boolean hasAllUniqueChars. Static methods don't need instances to be able to call them. So you would call it as UniqueChars.hasAllUniqueChars().
if you don't want method to be static then you would need to create new instance of UniqueChars class and then call it. new UniqueChars().hasAllUniqueChars() as others have pointed out.
Either construct an instance of the class that has the main method (possibly in main() itself), and invoke the method on it.
System.out.println(new UniqueChars().hasAllUniqueChars(input));
Or make hasAllUniqueChars() a static method.
Within an instance method or a constructor, this is a reference to the current object, which is non-static. You can't use non-static instance to static method.
I have the following code:
private static AppWidgetService mInstance = null;
public static void startRefresh() {
AppWidgetProvider.setRefreshingState(mInstance
.getApplicationContext());
AppWidgetManager.refreshHandler(mInstance.getApplicationContext());
}
It sometimes fails in runtime, and sometimes passes.
for the obviuse exception:
cannot call a non-static method from a static context
I'm confused as mInstance is static,
so its instance methods could be called from a static context. no?
then how come if sometimes fails?
From a static function, you can call only static function or use static variable. The linking is done at run time. So, though your compilation will be fine but at run time it will fail when call is made. Try making your function non static if you want to make that call.
OR
The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.
This problem arises when you call a static method from a non-static block/method. In your code both 'setRefreshingState()' and 'refreshHandler()' are static methods. To call theses methods you have to remove static from your method definition.
Can anyone please explain me why it is illegal for static method to invoke a non static method?
A non-static method is an instance method which acts upon a specific instance (object), and has access to that object's members. A static method is not associated with a specific instance.
It is legal to call a non-static method if you provide an instance:
class Foo
{
public static void f()
{
instanceMethod(); // Not OK - no instance provided.
this.instanceMethod(); // Not OK - no "this" in static methods.
new Foo().instanceMethod(); // OK because you provide an instance
}
public void instanceMethod() {}
}
because to call static method you don't need object of class, where to call non static method you need,
Static method is associated with class where non static method is associated with state (Object)
A non-static method is a method that executes in the context of an instance . Without an instance it makes no sense to call one, so the compiler prevents you from doing so - ie it's illegal.
If a method doesn't access any instance fields (aka instance variables) then it should probably be changed to be a static method.
non static methods have the implicit parameter this which static methods have not (because there is no this)
Alone because of this fact it would not be possible to call non-static methods form static ones.
Static methods belong to the class. Non static methods belong to an instance.
The non static method require an instance (this) to be executed on. The static method don't require any instance, since the class itself is the instance to which it belongs.
If I have 100 object instance, every nonStatic() method invocation affects the state of a different object. So every nonStatic method is 'different' because it acts on different entities.
At the same time, even if I have 100 instance of an object, calling staticMethod() doesn't affect any of those instances, because it can only affect the static members of the class.
There is 1 class and 1 instance of every static methods or static member variables.
For the non static members we have every single method associated to the class instance.
So if you would be able to call the nonStatic method, from a static method, which of the 100 instance would be affected? You see: it doesn't make sense.
Static methods are connected with the class.
Regular methods are connected to the object.
In static method there is no object on which you can run the non-static method.
It is not illegal to call a non-static method from a static method. Whenever you invoke a static method there is no need of an instance of a class whereas in case of non-static case you do.
We have self registering subclasses of 'Handler' which we want to access through Subclass.me(). Is something similar to this possible in Java: ?
public class Handler{
static Vector<Handler> register=new Vector<Handler>();
public static Handler me() {
return register.get( this.class);// TODO
}
}
public class SubClass extends Handler{
SubClass(){register.add(this);}// OK
}
To clarify the question: Is it possible to retrieve the CLASS when calling a static java method? this.class obviously doesn't work, because 'this' is not available.
Static methods belong to the class. They cannot be overridden.
MyClass.myStaticMethod()
is the only correct way of accessing a static method.
In java, you cannot make a static reference to the non-static method/variable. So,
If you want to access a non-static method / variable then you must
create an instance of the class first.
If you are going to access a static method / variable then you can
access it directly through the class name without creating a
instance.
Because, the static method and variable are belong to the Class not to the Instance while the non-static method and variable are belong to the Instance not to the Class.