Results 1 to 5 of 5
  1. #1
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Simple example of classes and values

    Can someone give me a quick and simple example of how you get a value from another class?

    I mean like you have a value i the second class: int a=1.
    and you have a value in the main class: int b;
    then you wanna set value b to a (1)

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Simple example of classes and values

    There are several ways of getting information from one place to another. I would suggest you look into the concept of getters and setters, i.e. methods with the only function of getting and setting variable values. There are of course times when other solutions are better, but understanding this will get you far.

    Here is a simple program that displays how it works.

    Main method:
    Java Code:
    public class GetAndSetDemo {
    	
    	public static void main(String args[]){
    		Person person = new Person("Ytrewc", 20); 
    		System.out.println("Hi my name is " + person.getName() + " and I am " + person.getAge() + " years old.");
    		person.setAge(21);
    		System.out.println("Sorry, I mean that I am " + person.getAge() + " years old.");
    	}
    }
    Person class:
    Java Code:
    public class Person {
    	
    	String name;
    	int age;
    	
    	public Person(String name, int age){
    		this.name = name;
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    Try adding you own setName() method and experiment.

    Cheers,
    Z!

  3. #3
    ytrewqc is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Simple example of classes and values

    Thanks, that was userful

    but is it possible to get a number from the other class?
    in the example you still set the values in the main class

    can you set the values in second class and print them from the main class?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple example of classes and values

    Don't post the same question repeatedly; you already asked it here: How do i get a value from another class

    Don't do that anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Simple example of classes and values

    Quote Originally Posted by ytrewqc View Post
    Thanks, that was userful

    but is it possible to get a number from the other class?
    in the example you still set the values in the main class

    can you set the values in second class and print them from the main class?
    I don't understand what you mean with this question. You can define the variables, such as age, in the Person class. Just set it to a definite value. You can access it from the main class with the getAge()-method. I just set the age in the main class for you to see how the setAge()-method works.

    You can have a constructor in the Person class with a default person, and you don't have to set anything in your main, you can just access it through the getters you've created.

    Cheers,
    Z!

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2012, 07:46 AM
  2. Simple 3 classes and incorect output
    By evonevo in forum New To Java
    Replies: 4
    Last Post: 12-08-2011, 08:10 PM
  3. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 06:49 PM
  4. Getting values from other classes
    By StealthRT in forum New To Java
    Replies: 4
    Last Post: 09-11-2008, 03:32 AM
  5. [SOLVED] Simple Q: Values between Classes
    By a45b22chp in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 05:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •