자바공부하기 77

 

NewStaticTest.java

 

class NewStaticTest

{

static void staticTest()

{

System.out.println("staticTest()");

}

void newTest()

{

System.out.println("newTest()");

}

public static void main(String[] args)

{

NewStaticTest.staticTest();

NewStaticTest nt = new NewStaicTest();

nt.newTest();

new NewStaticTest().newTest();

}

}

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

자바공부하기 79. 다형성이란?  (0) 2015.05.19
자바공부하기 78. 계산기 만들기  (0) 2015.05.18
자바공부하기 76. static  (0) 2015.05.14
자바공부하기 75. static  (0) 2015.05.12
자바공부하기 74. Static  (0) 2015.05.11

자바공부하기 76

 

static 멤버 메서드(클래스 메서드)

 

public class StaticMethodAccess {

private static int sint=100 ;

public int nint = 0 ;

public static void setStaticInt(int x){

sint = x ;

}

public static int getStaticInt(){

return sint;

}

public static void main(String[] args){

StaticMethodAccess.setStaticint(33333);

int s = StaticMethodAccess.getStaticInt();

System.out.println("static값은:" +s);

}

}

 

static 멤버 메서드도 객체생성이전에 접근이 가능

 

static 멤버 메서드에는 일반 멤버필드를 사용 할 수 없음(객체 생성 없이)

- 일반 멤버필드는 객체가 생성되면서 메모리를 할당 받음

- static 멤버 메서드는 객체와 연관 없이 사용 (어떤 객체와 연관될지 알 수 없음)

- 다라서 static 멤버 메서드에서는 일반 멤버필드를 사용할 수 없음

- 또한 일반 멤버 메서드도 사용할 수 없음

 

static 초기화 블록

 

 

- StaticTime 클래스명이 사용되는 순간 static블록이 실행

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

자바공부하기 78. 계산기 만들기  (0) 2015.05.18
자바공부하기 77. static  (0) 2015.05.14
자바공부하기 75. static  (0) 2015.05.12
자바공부하기 74. Static  (0) 2015.05.11
자바공부하기 73. Static  (0) 2015.05.11

자바공부하기 75

 

static

 

- 두 번째 루프

- StaticTest 생성자에서

> sint : 메소드영역 ( 계속 증가 )

> nint : 힙영역 참조

 

 

 

 

 

- 객체를 생성하지 않고 StaticAccess.sint로 값을 할당

> 객체생성여부와 상관없이 static메모리에 접근가능

> 그러나 인스턴스 변수의 경우 접근 불가능

  실체가 힙에 생기지 않았다.

 

- 사용방법

> 클래스이름.static변수이름 ex) StaticAccess.sint

 

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

자바공부하기 77. static  (0) 2015.05.14
자바공부하기 76. static  (0) 2015.05.14
자바공부하기 74. Static  (0) 2015.05.11
자바공부하기 73. Static  (0) 2015.05.11
자바공부하기 72. Static  (0) 2015.05.11

자바공부하기 74

 

Static

 

- 첫번째 루프

- StaticTest 생성자에서

*  sint : 메소드영역

* nint : 힙영역참조

 

 

 

 

- 두번째 루프

- StaticTest 생성자에서

* sint : 메소드 영역 ( 계속증가)

* nint : 힙영역참조

 

 

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

자바공부하기 76. static  (0) 2015.05.14
자바공부하기 75. static  (0) 2015.05.12
자바공부하기 73. Static  (0) 2015.05.11
자바공부하기 72. Static  (0) 2015.05.11
JAVA공부하기 71. JVM 메모리 모델  (0) 2015.05.11

자바공부하기 73

 

Static 멤버 필드 (클래스 필드)

 

public class StaticTest{

private static int sint=0;

private int nint = 0;

public StaticTest(){

sint = sint +1;

nint = nint +1;

}

public void sayMember(){

System.out.println("sint:" + sint + "nint:" + nint);

}

public static void main(String[] args){

for(int i=0; i<10; i++) {

StaticTest s= new StaticTest();

s.sayMember();

}

}

}

 

- sint는 0으로 설정

- sint가 메소드영역에 생김 

 

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

자바공부하기 75. static  (0) 2015.05.12
자바공부하기 74. Static  (0) 2015.05.11
자바공부하기 72. Static  (0) 2015.05.11
JAVA공부하기 71. JVM 메모리 모델  (0) 2015.05.11
JAVA공부하기 70. JVM 메모리 모델  (0) 2015.05.08

자바공부하기 72

 

static

 

static 키워드

 

- static 멤버 필드(클래스 필드)

: static으로 정의된 멤버변수는 전역(공유)의 뜻한다

: 모든 객체에서 공통으로 사용하는 메모리

: 메서드 내에는 static변수를 선언할 수 없음

 

- static 멤버 메서드 (클래스 메서드)

: 자동으로 final 메서드가 됨(overriding불가능)

 

- static 초기화 블록

: 객체가 생성되기 전에 static영역의 메모리를 제어하기 위한 블록

 

- static 메모리는 객체가 생성되기 전에 클래스명으로 접근이 가능

 

JAVA공부하기 71

JVM 메모리 모델

 

레지스터

- JVM 상태 유지

 

실행 풀

- 심볼 테이블, 숫자 리터럴, 상수필드 유지

 

public class Helloworld

{

public static int f(int arg)

{

int i = 10;

int f = 3;

return i*arg*f;

}

 

public static void main(String[] args)

{

int i = 3;

f(i);

}

}

 

 

 

 

JAVA공부하기 70

JVM 메모리 모델

 

* Point 클래스 로드

* Point 내의 멤버변수가 Heap 영역에 생성 (static 변수는 메소드 영역에서 생성)

* 디폴트 생성자가 호출

 

 

 

 

* move() 메소드 호출

* Move() 메소드 지역 변수 및 매개변수는 스택에서, 클래스 멤버변수는 힙에서 참조

 

 

 

 

+ Recent posts