This question already has answers here:
When to use static methods
(24 answers)
Closed 9 years ago.
Specifically using Java. What makes a function static? what does that mean? How should you choose to make the function static or not?
My while loop is broken because "non-static method hasPrecedence(java.lang.String,java.lang.String) cannot be referenced from a static context"
A static method is called outside of a specific instance of a class. For example, in the Java library, the Math.sqrt() function is not associated with a particular object.
In contrast, non-static methods must be called with a specific object. For example, a toUpperCase() method is defined for String objects:
String str = "Hello World.";
String upperStr = str.toUpperCase();
System.out.println(upperStr);
Notice that instead of calling String.toUpperCase() I used str.toUpperCase().
A static method is called outside of any specific object. A non-static method is called with a specific object to operate on.
A function is static if there is only one shared among all instances of a class. Generally, a static function is used for "utility" work. By this I mean, the function does some work for you that does not/should not require making an instance of the class. A static function therefore does not make use of or have access to instance variables or instance methods.
Related
This question already has answers here:
What is "static"?
(7 answers)
What does the 'static' keyword do in a class?
(22 answers)
Closed 9 months ago.
In Java we can use
non-static variables inside non-static methods without creating an
instance within the same class.
static variables inside non-static methods without having an issue within the same class.
static variables inside a static a static method without having any issue within the same class.
I just want to know why we can not use a non static variable inside a static method within the same class.
It wouldn't make sense. Static methods are about belonging to the class, rather than individual instances. So, they don't have access to any instance (i.e. you can't use this™ inside them).
Things that aren't static are bound to individual instances.
This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Difference between Static and final?
(11 answers)
Closed 2 years ago.
I am confused about when to use Static variables/methods and exactly what they do. I know Static means that the variable or method becomes a part of the class and not an instance, but when do we use this? Also, if a variable is static, does that mean it is immutable like a final variable? Basically what is the use of Static?
You will usually use static when your variables are general and are not supposed to be part of each instance separately, take as an example the number pi, if you create a Math class, you will set pi as a static final variable, and also the methods will be declared as static, because you don't want to create a class instance everytime you want to use the power function. If your function doesn't use the properties of the class it will probably be static.
Another use I found useful, when you create a class within another, you should declare the inner class as static, otherwise you will need an instance to create instance of the inner class
This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 8 years ago.
I have learned that fields are like global variables which can be accessed by the methods inside the same class. I have done it this way before and never had a problem. I have now a class where I have some fields but the methods cannot access them without having to make them static fields. I get the error "cannot make static reference to non-static..."
I thought static was to access fields on other classes without having to create an object reference to the class. The only difference I have with this code is that I have a single class and my main() method within this class. Does having main() inside this class make a difference?
A static member only exists once for the class itself as opposed to regular class members which are distinct per instance of your class.
Having a main() method does not impact the behavior of your static members, however static methods can only access static members while non-static methods can access both static and non-static class members.
You can't access non static instance inside static method. I think you are trying to access class variable inside main method directly, i.e.
class A
{
int x;
main() method
{
x;//Not accessible here,, create instance of class and access it.like
A a=new A();
a.x;
}
}
Static (methods,variables,classes, etc) belongs to Class not to the particular instance of the class. We define as static when the behaviour or state does not depend on any particular instance of the class. For Example "generate a Random Number " it does not depend on the instance, it always generate a number regardless of instance such behaviour can be defined as static.
Regarding the error, posting you code will helpful to give better solution.
Refer the below link to know more about Static and non-static
http://javarevisited.blogspot.in/2012/02/why-non-static-variable-cannot-be.html
This question already has answers here:
Why keyword 'this' cannot be used in a static method?
(13 answers)
Closed 9 years ago.
I knew that we can not use this keyword in a static method, But I got confused why we can not use this inside static blocks orstatic methods. Even the same case with super(). Could anyone shed light on this?
Thanks
this and super refer to the current instance and the parent instance respectively.
Within any static context, whether it is a static block or a static method, there is no instance to refer to, and therefore the keywords are not permitted.
super() is a call to the parent instance's no-arg constructor, and is only permitted as the first statement in a constructor, which disqualifies it from appearing in any static context.
According to the doc
Within an instance method or a constructor, this is a reference to the
current object — the object whose method or constructor is being
called. You can refer to any member of the current object from within
an instance method or a constructor by using this.
But the static methods are related to the Class not to the object. in static methods you dont have any instance.
Static methods and blocks are, by definition, linked to the class and not any instance of this class.
As this refers to the current object instance, it is perfectly normal that you cannot use it in any static initialization block or method.
Because this points to an instance of the class, in the static method/block you don't have an instance.
according to Oracle
Within an instance method or a constructor, this is a reference to the
current object — the object whose method or constructor is being
called.
So, in a static class you have NO instance variables-objects created. That's why you cannot use the this keyword
Static blocks can be used to initialize static variables for example.
Static merthods are not working within an instance scope either.
Both are related to only the class and have nothing to do with an instance of that class in any way.
To answer this we should consider what static means - it means that this bit is put in a 'static' place in memory, it exists only once.
So every class has static bits and non-static bits. The static bits exist once and the non-static bits can exist many times (i.e. lots of different instances). The word 'this' can then be thought of as shorthand for 'this version of the non-static bits'.
In a static context we have no version of the non-static bits to refer to (or to be more precise we don't know which version we should refer to!) so we have no this. The same argument can be made for super.
This question already has answers here:
Why can't implementing classes define an overriding method as static?
(4 answers)
Closed 9 years ago.
Why can't we override a non-static method to make it static. I got this error when trying to do it :
This static method cannot hide the instance method from aSuperClass.
Many thanks!
Overriding means providing a new implementation in the child class for instance method, Instance method may or may not work with the class instance members. Its not logical to override them and redefine them as static. static means the method is related to the class, not to the instance of the class.
You can't do that in C# as well as Java.
For C# Look at override - MSDN
You cannot use the new, static, or virtual modifiers to modify an
override method.
Static methods are not overridden in the way Polymorphism works. However when you try to override a static method Java allows the concept of Name Hiding.
Now coming to your question. A Static method can be invoked both by Class Name and Object name. If you override a non static method with a static one how will the compiler know which one to invoke.
static void f(){}
A obj = new A();
A.f() and obj.f() will both invoke the static method. If you put a Non-Static f() here the confusion starts for obj.f().