개발입문/JAVA

[30일코딩] 클래스와 인스턴스

haloaround 2017. 5. 5. 11:18


클래스 


클래스 변수/메소드        same static variable

인스턴스를 만들지 않아도 클래스 공통으로 사용할 수 있는 변수/메소드

e.g. Car 의 minSpeed 는 모든 차 불문하고 0

인스턴스 변수/메소드        distinct value of each instance

각 인스턴스가 다르게 가질 수 있는 변수/메소드

e.g. Car 의 maxSpeed 는 차종마다 다르다. 스포츠카는 200 일반차는 150


생성자 Overloaded Constructor

- allows multiple constructor 

- Default Constructor

- Parameterized Constructor


메소드

Scope of this function to access it

Generic

Return type

Parameter (argument)

게터/세터메소드

getter/setter method of instance variables




class MyClass{

    int instanceVariable;

    ...

    void setInstanceVariable(int value){

        this.instanceVariable = value;

    }

    dataType getInstanceVariable(){

        return instanceVariable;

    }

}


Structuring code this way is a means of managing how the instance variable is accessed and/or modified.