![]() The syntax of the static variable in java is similar to the syntax declaration of the instance variable. For example, the default value of the int data type is 0, and the boolean data type is false. When the static members have not initialized the initial value to the static members the default value is automatically initialized to the particular static member in java. The static field or static variables are those variables that are equally shared among all the instances of the class, like the static methods they are also initialized using the static keyword and accessed by the classname of the class. The static field(static variables) and static method are the two types of static members in java. Let's understand the structures of the static method and the static variable java. That's why the main method has to be static, so JVM can load the class into memory and call the main method. The main() method is of static type because when java runtime starts, there is no object of the class present. You are very familiar with one of the most widely used static methods i.e the main() method of every Java program. If yes, then the method should be static. If you are confused between a static method and instance method, always remember one rule "Does it make sense when you call a particular method without creating the object of the class ". Yes ! we can but this is not a good way because the memory consumption in more in this case due to individual memory allocation of the schoolname variable for every object in the heap memory. Now you are thinking we can hardcore the schoolname in the class. Because static members are equally shared among all the objects of the class. In this case, we can consider schoolname as a static variable. Every object of the class represents about the student, Now think which information is common in between of all the students ?. Let's suppose you want to design a class 'Student' that contains all the necessary information like name, age, class, school name, and rollno about the particular student that is studying in the school. Let's understand static methods and variables with the help of a real-world example. This article covers the properties of the static methods and members in java with real-life examples.This article defines the static methods and members in java.We cannot declare the static methods and members inside the body of any kind method of the class, and represented by the prefix static keyword in the class. They are used to represent the state of the class rather than the state of an object. The static methods and members can be accessed without creating the object of the class. The static methods and members are the members of the class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |