Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass values

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US
Hi,

I'm new using java and have a question:

I have two classes called class1, class2

In class1 I import class2 how can I set up a variable in class1 and then get the value in class2

Thanks
 
the best way, is to learn basics of Java

Ion Filipski
1c.bmp
 
Take a tutorial ...


As a hint though :

Code:
public class One {
	prvate String message = "hello";
	
  	public String getMessage() {
		return this.message;
  	}

  	public String setMessage(String message) {
		this.message = message;
  	}  	
}

public class Two {
	public static void main(String args[]) {
		One one = new One();
		
		System.out.println(one.getMessage();
		
		one.setMessage("World");
		
		System.out.println(one.getMessage();
		
		
	}
}
 
Sorry, should be (void return type on setMessage()) :

Code:
public class One {
    prvate String message = "hello";
    
      public String getMessage() {
        return this.message;
      }

      public void setMessage(String message) {
        this.message = message;
      }      
}

public class Two {
    public static void main(String args[]) {
        One one = new One();
        
        System.out.println(one.getMessage();
        
        one.setMessage("World");
        
        System.out.println(one.getMessage();
        
        
    }
}
 

Sedj,

Thank you very much for your help it is nice to know the still some people with class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top