Heim >Java >javaLernprogramm >Statische Methode in Java
Static Method in Java is a method that is part of a class but is not considered an instance of the class; rather, the static method in java can easily be created and implemented without any invocation of instances. The static method can access any data member of the class and can make any manipulation to the data members or can put any value as an input despite for the fact that the member variable to be accessed should have the scope of the variable in the class and method to be static only.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Representation of the static method in Java is as follows:
public static void syntax_ex (String_name) { Body of the program for execution. }
Given below are the examples of Static Method in Java:
This program demonstrates the static class with its specific members, which tries to rectify the marks in the subjects mentioned; basically, it shows that without creating any object or without any instantiation, it is able to create and access the fields or member variables of the class which is within static scope.
Code:
public class E_Static_Method_Ex { int roll_no; String stdnt_name; static String subject = "Maths"; public void rectify() { subject = "English"; } E_Static_Method_Ex(int roll, String name) { roll = roll_no; name = stdnt_name; } void represent() { System.out.println(roll_no+""+stdnt_name+""+subject); } public static void main(String[] args) { String mrks = E_Static_Method_Ex.subject; System.out.println(mrks); } }
Output:
This program demonstrates a very significant point which needs to be kept in mind while executing any static method code, which is like the arguments passed and declared in the class should be defined within the scope of static or should have initialized with the static keyword so that accessing the field or the member variable becomes easy and there remains no compilation error as represented in the given program.
Code:
public class Restrcn_Static_Class { int bi = 30; String name = "Welcome Everyone!"; public static void main(String[] args) { System.out.println(bi); System.out.println(name); } }
Output:
This program demonstrates the static variable that becomes accessible for the static class if the fields are declared and defined with static as a modifier; otherwise, the scope of static won’t get satisfied, and it will throw compilation error like the earlier example.
Code:
public class Static_Var_1 { static int cnt=0; Static_Var_1(){ cnt++; System.out.println(cnt); } public static void main(String[] args) { Static_Var_1 d1=new Static_Var_1(); Static_Var_1 dd2=new Static_Var_1(); Static_Var_1 dd3=new Static_Var_1(); Static_Var_1 dd4=new Static_Var_1(); Static_Var_1 dd6=new Static_Var_1(); } }
Output:
Static Method in Java is a useful method in a class and has a clear difference when compared with the instance method as static method gives programmers flexibility by making them free from object creation, i.e. from the method of object instantiation. It helps in making the entire static class dynamic and versatile in nature, unlike the instantiation method.
Das obige ist der detaillierte Inhalt vonStatische Methode in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!