Anyone explain me static keyword in java . All functions and uses. This make me confused a lot. | Sololearn: Learn to code for FREE!
0

Anyone explain me static keyword in java . All functions and uses. This make me confused a lot.

Hello solo learner's As we know java is very difficult as compared to python. One thing make it hard that is concepts of oops and classes basic language. In this hard programming another things hard that's is static keyword. Whenever I write program my code editor suggest me to use Static keyword and sometimes says to remove. I don't know where it use and where not to use . Anybody here who knows everything about Static keyword use in java. So explain me please in easy terms. Buterbrod:) -----thankyou in advance---------:)

5th Jul 2024, 6:21 AM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
3 odpowiedzi
+ 3
Let's assume you have written a dog class. In this dog class you have a method bark. If you want to use this method, you must create an instance of this class with the keyword new. But now you want to add another method to your class. A method that returns all instances of your dog class. So you have added a list to your class and a new method. This is the moment when static comes into play. You make this method static because you don't want to create an additional instance of your class. You want to use this method directly. Static methods belong to the class in which they are written. But you can use them without creating a new instance. Here is the SoloLearn lesson about static methods. https://www.sololearn.com/learn/o-Java/2159/?ref=app
5th Jul 2024, 7:16 AM
Stefanoo
Stefanoo - avatar
+ 2
The 'static' keyword is used to declare a variable or method to be part of the class itself rather than an instance of it. For example, take the standard Java class 'Math'. To use its methods you can simply specify the name of the class and the method you want to use Math.sqrt(9); // 3 You don't need to instantiate the class using the 'new' operator since the 'sqrt' method is static. However, you can still use such methods through an instance of the class, although this is not required https://sololearn.com/compiler-playground/cXxJIHtro1jg/?ref=app What about variables? They work similarly, if you have a static variable then it will belong to the class itself and will be shared by all instances of that class https://sololearn.com/compiler-playground/cvxmU5VS6FQt/?ref=app As we can see, changing a static variable through one instance changes its value for all instances.
5th Jul 2024, 9:12 AM
Buterbrod:)
Buterbrod:) - avatar
+ 2
*Addition to previous post* When declaring the 'main' method, you can see that it must be static, this is due to the fact that the environment cannot create an instance of the class in this method, but can call it statically, it will look something like this MainClass.main(stringArray); There is another important point: since static methods are common to all instances, they do not have access to non-static members of the class. This is because such members may have different states in different instances. So if you want to use any method or variable in the main method, you must declare it as static.
5th Jul 2024, 9:23 AM
Buterbrod:)
Buterbrod:) - avatar