+ 18
Why should you make a method static?
why should you define a method static? Because the Objects of that spezific class could access this method anyway, so why would you define it static so every class object could access it?
21 Answers
+ 17
Sometimes a method or class is used a lot throughout your project that you don't want to make a lot of instances of it, so you just make it static.
System class in java for example, is static because you use it, but you don't need to make an instance of it.
You could also for example create a static int that since it's static, it belongs to the class itself and it counts the number of instances made by increment it by one inside the constructor.
+ 17
simple explanation based on its place of usage:
local variable:
static local variable is allocated in data segment instead of call stack...which means lifetime of the variable is extended to complete program, it's value remains throughout the program.
global variable or functions:
static makes functions/global variables not accesaible from outside file, scope is limited to that file.
Inside class :
static inside a class for data or functions implies every object has same copy of static data, static data or functions can be accessed with class name.
+ 15
"static" , which declares the method as one that belongs to the entire class and not a part of any objects of the class. The main must always be declared as static since the interpreter uses this method before any objects are created.
+ 8
if a method's functionality is for class/program level then we declare it as static. And
if a method's functionality is for object level then we declare it as non-static
example:-
main() method
In java function can only be member functions.
A member function can be static or non-static.
A non static member function can only be
accessed using object of the class.
If main would have been a non static member
function then for invokation it would have
required object of the class.
A Java Program begins with main.
No object exists before main.
So main must be invoked without any object
Hence main is declared as a "static member"
function, because static functions can be
called without any object.
+ 6
Static is used for not making its copy for objects. Understand it by an practical example that when you make a program in java after compiling it you can see that you have to create object before you run the program if you have used objects in that program...on the other hand if you have used "static" keyword after every method and global variables you will see that you dont have to create any object to run you program. That's all I know hope you find it useful
+ 6
If you declare a method as static then you don't have to call it by creating an object of the class. You can directly call that method via class name.. Another reason is because as soon as you create an object it is stored in the Heap memory and if you are not creating an object of a class that means you are freeing memory space which allows you to run your code faster.
+ 4
static methods and static variables are only used for the same classes where they are declared like the class Math. Static modifier is used to a global use of your software, an exaple:
Math.random();
Math.round(float x);
public static Scanner sc=new Scanner(System.in);
/* this scanner will be used to all of your classes */
the difference between intance method and static method is:
-instance method --> used to the class where it is declared
MyClass mc=new MyClass();
mc.doSomething();
-static method-->used for global uses using NAMECLASS.STATICMETHOD
int x=mc.getVal();
int y=(int)(Math.random()* x);
System.out.println(y);
+ 4
because static methods belongs to class not to objects of that class. if you want to use a method that belong to some class and you don't want to intiate the object of that class then you use static methods. they are availabe with class name
+ 3
If we use 'static' keyword in a method then we can easily call that method without creating any object. We can easily call it by class name. for eg,
class_name.method_name
+ 3
Just imagine that you have to build an application for a garage, and you want to count how many cars the garage contain at the moment, the best solution is to have a static methos that count for you after everg class garage/car call
+ 3
it also took me a while to understand the static concept!!
+ 1
Because, so that all the instances of the same class can access the same (or updated) data. Suppose we need to calculate how many instances of a class have been created.
Example :
class Person {
String name;
int age;
float weight;
Person() {
static int count = 0;
count++;
/* Increments count variable by 1 each time a new instance of Person class is created. */
}
}
Note :- The variable (or method) declared as static can be initialized only once. Here the "count" variable is declared and initialized only when the very first instance of Person class is being created. After that it just keeps incrementing its value by 1 whenever a new instance of the same class is being created. It does not overwrites it value by 0.
*** Hope this may help you. All the best. Keep Coding...!!! ***
+ 1
To understand why static "members" we should talk about "state" of the objects or instances of our types, like objects created from classes for example, so, the state of an instance of a class (object) is the value of all its "instance" members, among which static members are not, then, static members are "stateless", they don't require an instance to exist, and you cannot utilize them from an instance, that is why we can access Console.WriteLine(...) without create an instance of that class "Console" before. Static members are usefull to represent data or behavior that is or can be "general" or "persistent" or "immutable" from one instance to another, whenever we refer to the type it belong to. There can be even an static constructor per class, which will be execute before any instante constructor and can be used to initilize other static members. Hope you enjoy it.
+ 1
Make method static when
1. When you want to work (read, write, modify) with properties (declared using static fields) of the class itself.
2. When you want to provide Utility methods for other classes to consume like System.console.println, Math.Sqrt() etc.
3. For (pre)configuring default implementation of interface methods (since java8)
4. When you want to control the instantiation of class objects, like Singleton pattern (only one object of class throughout the lifetime of a program)
5. for use in main (entry point of your progtsm) so that it can called without creating object. Otherwise there would be catch 22 situation.
0
An instance method has its instance as an additional argument. That only makes sense when the instance can vary. You wouldn't add unnecessary arguments to your function either.
An alternative 1+ parametric functions could be extension methods, but Java doesn't have those.
- 1
و
- 1
if you have a method in the main class
you cant call this method if its not static
this is in the main method
- 1
code
- 1
Hello
- 2
The keyword static is used because before we access the classes the main method has to be accessed so by using static it is possible.