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;
}
}
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);
}
}
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);
}
}
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;
//
}
public static int count = 0;
//
}
MyClass.count = 10; //
MyClass obj = new MyClass();
obj.count = 20; //
MyClass obj = new MyClass();
obj.count = 20; //
public class MyClass {
public static int count1 = 0;
public static int count2 = count1 + 1;
//
}
public static int count1 = 0;
public static int count2 = count1 + 1;
//
}
count1 count2
final
publicprotectedprivate
Java
volatile
static
myStaticVariable""Upper Snake Case
MY_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;
}
//
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
h
135***[email protected]
Java
staticInt staticInt 1 random random random 1
public class StaticTest { private static int staticInt = 2; private int random = 2; public StaticTest() { staticInt++; random++; System.out.println("staticInt = "+staticInt+" random = "+random); } public static void main(String[] args) { StaticTest test = new StaticTest(); StaticTest test2 = new StaticTest(); } }h
135***[email protected]
362***[email protected]
362***[email protected]
stinkaroo
190***[email protected]
java
import java.io.*; public class Employee{ public Employee (String empName){ name = empName; } public void setSalary(double empSal){ salary = empSal; } public void printEmp(){ System.out.println("name:" + name); System.out.println("salary:" + salary); } public static void main(String args[]){ Employee empOne = new Employee("RUNOOB"); empOne.setSalary(1000); empOne.printEmp(); System.out.println(empOne.salary); } public String name; private double salary; }stinkaroo
190***[email protected]
528***[email protected]
public class StaticTest { private static int staticInt = 2; private int random = 2; public StaticTest() { staticInt++; random++; } public static void main(String[] args) { System.out.println(""); StaticTest test = new StaticTest(); System.out.println(" 1staticInt:" + test.staticInt + "----random:" + test.random); StaticTest test2 = new StaticTest(); System.out.println(" 2staticInt:" + test.staticInt + "----random:" + test.random); System.out.println(""); System.out.println(" :" + A.staticA); A a = new A(); System.out.println(" :" + a.staticA); a.toChange(); System.out.println(" 1:" + A.staticA); a.toChange2(); System.out.println(" 2:" + A.staticA); System.out.println(""); System.out.println(" :" + B.staticB); } } class A { public static String staticA ="A" ; // static{ staticA ="A1"; } // public A (){ staticA ="A2"; } // public static void toChange(){ staticA ="A3"; } public static void toChange2(){ staticA ="A4"; } } class B { public static final String staticB ; // static{ staticB ="B"; } }528***[email protected]
Smiley
448***[email protected]
public class StaticTest { private static int staticInt = 2; private int random = 2; public static void main(String[] args) { System.out.println(staticInt); StaticTest test = new StaticTest(); System.out.println(test.random); } }Smiley
448***[email protected]
GGBOND
110***[email protected]
,
,
,
,,
---
---
GGBOND
110***[email protected]
131***[email protected]
class A{ static int i; void change(int i1){i=i1;} } public class Test{ public static void main(String[] args){ A.i=10; A a=new A(); A b=new A(); System.out.println(A.i+","+a.i+","+b.i);//10,10,10 a.change(40); System.out.println(A.i+","+a.i+","+b.i);//40,40,40 b.i+=10; System.out.println(A.i+","+a.i+","+b.i);//50,50,50 } }131***[email protected]
oba
dyh***st.com
private static final double PI = 3.14;
+ + +
oba
dyh***st.com
LOYO
752***[email protected]
public class Test { public static int staticVar=0;//,static public int instanceVar=0;// public void VariantTest(){ staticVar++; instanceVar++; System.out.println("staticVar"+staticVar+",instanceVar="+instanceVar); } public static void main(String[] args) { for(int k=0;k<6;k++){ Test p=new Test(); p.VariantTest(); } } }:
LOYO
752***[email protected]
LOYO
752***[email protected]
staticstatic?
public class Xix { // public static String string="static"; // public String string2="static"; // public static void method(){ string="sss"; //string2="sss";, //method2(); System.out.println("static,static"); } // public void method2(){ string ="string1"; string2="string2"; method(); //static System.out.println("static,"); } public static void main(String[] args) { Xix x=new Xix(); x.method2();// x.method();// } }:
:
LOYO
752***[email protected]
259***[email protected]
package hello; // // public class Variable { public String instance = ""; public static String variable = ""; // public static String CONST = ""; // public static void main(String[] args) { String local = ""; // System.out.println(local); // Global global = new Global(); // System.out.println(global.instance); //new,, System.out.println(variable); //1() System.out.println(Global.variable); //2() } } class Global{ public String instance = ""; //, public static String variable = ""; Global(){ //new, //1 System.out.println(instance); System.out.println(variable); } public void Instance() { System.out.println(instance); //1(), System.out.println(Variable.CONST); } }259***[email protected]
233333
135***[email protected]
Java
public calls Student{ static String schoolName =""; // String classNane =""; // public void show(){ /**/ int age =7; String name ="" } }233333
135***[email protected]