+ 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?

6th Mar 2017, 8:01 AM
Andreas Wagner
Andreas Wagner - avatar
21 Réponses
+ 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.
6th Mar 2017, 4:49 PM
Shahar Levy
Shahar Levy - avatar
+ 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.
7th Mar 2017, 4:04 AM
manoj katari
manoj katari - avatar
+ 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.
13th Mar 2017, 5:23 AM
Jaseem Akhtar
+ 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.
6th Mar 2017, 11:53 AM
POJO
POJO - avatar
+ 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
6th Mar 2017, 8:27 AM
Raghav Gupta
Raghav Gupta - avatar
+ 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.
12th Mar 2017, 11:15 AM
Kashif shaikh
Kashif shaikh - avatar
+ 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);
7th Mar 2017, 9:07 AM
Vulcan Rider
Vulcan Rider - avatar
+ 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
13th Mar 2017, 10:48 AM
Muhammad Umair
Muhammad Umair - avatar
+ 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
7th Mar 2017, 2:51 PM
Mayank Gupta
Mayank Gupta - avatar
+ 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
12th Mar 2017, 11:00 PM
majdi abu salah
+ 3
it also took me a while to understand the static concept!!
14th Mar 2017, 5:53 AM
Olawale
Olawale - avatar
+ 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...!!! ***
11th Mar 2017, 6:02 PM
Shubham Agrawal
Shubham Agrawal - avatar
+ 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.
14th Mar 2017, 5:05 AM
Lesky Alfonso Martinez
+ 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.
10th Jun 2017, 8:02 PM
NeutronStar
NeutronStar - avatar
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.
6th Mar 2017, 10:23 PM
1of3
1of3 - avatar
- 1
و
10th Mar 2017, 3:52 PM
Oussama Loucif
Oussama Loucif - avatar
- 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
10th Mar 2017, 4:45 PM
Ahmed El_Sayed Gomaa
Ahmed El_Sayed Gomaa - avatar
- 1
code
10th Mar 2017, 4:54 PM
sajjad ali
sajjad ali - avatar
- 1
Hello
13th Mar 2017, 6:06 PM
Abhishek Dandriyal
Abhishek Dandriyal - avatar
- 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.
6th Mar 2017, 1:48 PM
sai teja
sai teja - avatar