2009-11-08 2 views

답변

0

두 함수 사이에서 변수를 공유하려는 경우 전역 변수를 사용하거나 포인터로 변수를 전달할 수 있습니다. 포인터

예 :

public void start() { 
    ArrayList a = new ArrayList(); 
    func(a); 
} 

private void func(ArrayList a) 
{ 
    a.add(new Object()); 
} 
0

이 질문이 무엇을 의미하는지 확실하지. 모든 공용 클래스는 공유되며 공용 메서드를 통해 액세스 할 수 있으면 모든 변수를 공유 할 수 있습니다.

+0

그들은 보호 된 것으로 공유 될 수 있습니다. 호출하는 메서드가 액세스 할 수있는 한 비공개로도 사용할 수 있습니다. – Spoike

0

VB sense에서 Java의 static field은 모든 클래스 인스턴스에서 공유됩니다.

classical sense에는 Java에 다양한 RPC, 서비스 및 데이터베이스 액세스 메커니즘이 있습니다.

1

"변수를 공유"하거나 다양한 방법으로 "데이터를 공유"할 수 있다는 의미에 달려 있습니다. 나는 네가 초보자라는 것을 받아 들여서 간단하게 만들 것이다. 짧은 대답입니다. 변수를 공유 할 수 있으며 아래에는 몇 가지 방법이 있습니다. 클래스 함수 속성과

void funcB(int x) { 
    System.out.println(x); 
    // funcB prints out whatever it gets in its x parameter 
} 

void funcA() { 
    int myX = 123; 
    // declare myX and assign it with 123 
    funcB(myX); 
    // funcA calls funcB and gives it myX 
    // as an argument to funcB's x parameter 
} 

public static void main(String... args) { 
    funcA(); 
} 

// Program will output: "123" 

공유 데이터의 매개 변수에 대한 인수로

데이터를 공유하면 객체에 클래스를 인스턴스화 할 때, 속성을 가진 클래스를 정의 할 수 있습니다

(예 : "new") 객체의 속성을 설정하고 전달할 수 있습니다. (

private static int count = 1; 
public int getCount() { 
     return count ++; 
    } 

마다이 유 호출 방법 getCount :

private Point createPoint() { 
    Point p = new Point(); 
    p.x = 1; 
    p.y = 2; 
    return p; 
} 

public static void main(String... args) { 
    Point myP = createPoint(); 
    System.out.println(myP.x + ", " + myP.y); 
} 

// Program will output: "1, 2" 
0

사용 정적 키워드 예 :

class Point { 
    public int x; // this is integer attribute x 
    public int y; // this is integer attribute y 
} 

당신은 다음과 같은 방법으로 사용할 수 있습니다 : 간단한 예는 매개 변수 클래스를 가지고있다), 카운트 계속 +1