Java

Java

type identifier [ = value][, identifier [= value] ...] ;

  • type --
  • identifier -- ,

int a, b, c; // inta bc int d = 3, e = 4, f = 5; // byte z = 22; // z String s = "runoob"; // s double pi = 3.14159; // pi char x = 'x'; // x 'x'

Java

  • Local Variables

    public void exampleMethod() {
        int localVar = 10; // 
        // ...
    }
  • Instance Variables0booleanfalsenull

    public class ExampleClass {
        int instanceVar; // 
    }
  • Class Variables static

    public class ExampleClass {
        static int classVar; // 
    }
  • Parameters

    public void exampleMethod(int parameterVar) {
        // 
        // ...
    }

RunoobTest instanceVar staticVar

method() paramVar localVar

main() RunoobTest method()

public class RunoobTest { // private int instanceVar; // private static int staticVar; public void method(int paramVar) { // int localVar = 10; // instanceVar = localVar; staticVar = paramVar; System.out.println(": " + instanceVar); System.out.println(": " + staticVar); System.out.println(": " + paramVar); System.out.println(": " + localVar); } public static void main(String[] args) { RunoobTest v = new RunoobTest(); v.method(20); } }

: 10
: 20
: 20
: 10

Java

Java

Java

accessModifier returnType methodName(parameterType parameterName1, parameterType parameterName2, ...) {
    // 
}
  • parameterType --
  • parameterName --

  • Java

  • Java

public class RunoobTest {
    public static void main(String[] args) {
        int a = 10, b = 20;
        swap(a, b); // swap
        System.out.println("a = " + a + ", b = " + b); // ab
    }
   
    public static void swap(int x, int y) {
        int temp = x;
        x = y;
        y = temp;
    }
}

a = 10, b = 20

Java

Java

type variableName;
  • type --
  • variableName --

  • Java

  • int count;

  • Java JVM

  • JVM

public class LocalVariablesExample {
    public static void main(String[] args) {
        int a = 10; // a
        int b;     // b
        b = 20;    // b
       
        System.out.println("a = " + a);
        System.out.println("b = " + b);
       
        //
        // int c;
        // System.out.println("c = " + c);
    }
}

a b

age pupAge()

package com.runoob.test; public class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println(": " + age); } public static void main(String[] args){ Test test = new Test(); test.pupAge(); } }

:

: 7

age

package com.runoob.test; public class Test{ public void pupAge(){ int age; age = age + 7; System.out.println(" : " + age); } public static void main(String[] args){ Test test = new Test(); test.pupAge(); } }

:

Test.java:4:variable number might not have been initialized
age = age + 7;
         ^
1 error

  • 0 false null
  • ObjectReference.VariableName

accessModifier type variableName;
  • accessModifier -- publicprotectedprivate
  • type --
  • variableName --

int 0boolean false

a b

public class RunoobTest {
      private int a; // a
      public String b = "Hello"; // b
     
      public static void main(String[] args) {
         RunoobTest obj = new RunoobTest(); //
         
          obj.a = 10; // a10
          System.out.println("a = " + obj.a);
         
          obj.b = "World"; // b"World"
          System.out.println("b = " + obj.b);
      }
  }

:

a = 10
b = World

name salary

Employee.java

import java.io.*; public class Employee{ // public String name; // private double salary; //name public Employee (String empName){ name = empName; } //salary public void setSalary(double empSal){ salary = empSal; } // public void printEmp(){ System.out.println(" : " + name ); System.out.println(" : " + salary); } public static void main(String[] args){ Employee empOne = new Employee("RUNOOB"); empOne.setSalary(1000.0); empOne.printEmp(); } }

:

$ javac Employee.java 
$ java Employee
 : RUNOOB
 : 1000.0

Java

static

count 0

public class MyClass {
    public static int count = 0;
    //
}

MyClass.count = 10; //
MyClass obj = new MyClass();
obj.count = 20; //

public class MyClass {
    public static int count1 = 0;
    public static int count2 = count1 + 1;
    //
}

count1 count2

final

publicprotectedprivate

Java

volatile

static

  • myStaticVariable

  • ""Upper Snake CaseMY_STATIC_VARIABLE

public class MyClass {
    //
    public static int myStaticVariable;

    //
    public static final int MAX_SIZE = 100;

    //
    public static final String employeeName;

    //
    public static double defaultInterestRate;
}

AppConfig APP_NAMEAPP_VERSION DATABASE_URLURL final

main()

AppConfig.java

public class AppConfig { public static final String APP_NAME = "MyApp"; public static final String APP_VERSION = "1.0.0"; public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb"; public static void main(String[] args) { System.out.println("Application name: " + AppConfig.APP_NAME); System.out.println("Application version: " + AppConfig.APP_VERSION); System.out.println("Database URL: " + AppConfig.DATABASE_URL); } }

:

Application name: MyApp
Application version: 1.0.0
Database URL: jdbc:mysql://localhost:3306/mydb

Counter count Counter

getCount()

main() Counter

Counter.java

public class Counter { private static int count = 0; public Counter() { count++; } public static int getCount() { return count; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = new Counter(); System.out.println(": " + Counter.getCount()); } }

:

: 3

JavaJava