在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);
}