Car car = new Car();public Class Profile {
String name;
int age;
public static void main(String args[]) {
}
}
public void setName(String str) {
name = str;
}
public void setAge(int val) {
age = val;
}
public Class Profile {
String name;
int age;
public void printName() {
System.out.println(name);
}
}
public Class Profile {
String name;
int age;
public void printName() {
System.out.println(name);
}
public void printAge() {
System.out.println(age);
}
}
main 메서드에서 Profile 객체 선언
public Class Profile {
public static void main(String[] args) {
Profile profile = new Profile();
}
}
setName메서드를 사용하여 “Min”값을 넘기고, setAge()사용하여 20값을 넘기자
public class Profile {
String name;
int age;
public void printName() {
System.out.println(name);
}
public void printAge() {
System.out.println(age);
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Profile profile = new Profile();
profile.setName("Min");
profile.setAge(20);
}
}
printAge(), printName메서드를 통해 “My name is Min”, “My age is 20”을 출력하자
public class Profile {
String name;
int age;
public void printName() {
System.out.println("My name is " + name);
}
public void printAge() {
System.out.println("My age is " + age);
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Profile profile = new Profile();
profile.setName("Min");
profile.setAge(20);
profile.printName();
profile.printAge();
}
}
public Class Car {
int speed;
int distance;
String color;
public Car() {}
}Car tesla = new Car();
Car kia = new Car(); Car이라는 클래스를 통해 tesla, kia 객체를 생성했다. 클래스는 여러 객체의 공통된 특성을 모아놓은 틀이다. 또한 클래스를 통해 객체를 생성해야만 클래스에 정의한 행위나 상태를 변경하거나 수행할 수 있다.public Class Car {
int speed;
int distance;
String color;
public Car() {}
public void speedUp() {
speed ++;
}
}Car kia = new Car();
kia.speedUp();public void speedUp() {
speed ++;
}메소드 내에서 선언한 변수
메소드의 매개변수도 포함
지역변수는 임시 변수로서, 메소드가 스택에 들어있는 동안만 유효하다.
아래 예제에서 지역변수는 x, i, b
public void foo (int x) {
int i = x + 3;
boolean b = true;
}
인스턴스 변수