Wood Chen

Local and Global Variables in Java

0 comments64 views64 words

This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

1.Add staic before a field to make it static

package Number;
public class Frist{
static String s1=“hello”;
public staitc void main(String[]args){
Stirng s2=“java”;
sysout s1;
sysout s2;
  }˙
}

Class fields are global variables within the class (member variables), while variables declared inside methods are local variables. They differ in where they are declared.

main is the main method, public=access modifier, static=static modifier, void=return type modifier. In Java, main must use these 3 modifiers. String[]args=an array of strings.

Last updated 2024-01-14

Related posts

Comments 0