在Java中定義變量的方法有以下幾種:
int num = 10;
String name = "John";
var num = 10;
var name = "John";
final int MAX_NUM = 100;
final String GREETING = "Hello";
public void printNum(int num) {
System.out.println(num);
}
public class Person {
private String name;
private int age;
// 構造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public void printMessage() {
String message = "Hello World";
System.out.println(message);
}