자바공부하기 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

자바공부하기 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 메모리는 객체가 생성되기 전에 클래스명으로 접근이 가능

 

+ Recent posts