Results 1 to 5 of 5
- 08-12-2012, 11:55 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
- 08-12-2012, 01:55 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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:
Person class: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."); } }
Try adding you own setName() method and experiment.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; }
Cheers,
Z!
- 08-12-2012, 04:28 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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?
- 08-12-2012, 04:38 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-12-2012, 04:44 PM #5
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Simple example of classes and values
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
-
Assigning values to a variable (simple question, I believe)
By mikehaas763 in forum New To JavaReplies: 3Last Post: 05-04-2012, 07:46 AM -
Simple 3 classes and incorect output
By evonevo in forum New To JavaReplies: 4Last Post: 12-08-2011, 08:10 PM -
Passing values between classes problem.
By alin_ms in forum New To JavaReplies: 8Last Post: 12-12-2008, 06:49 PM -
Getting values from other classes
By StealthRT in forum New To JavaReplies: 4Last Post: 09-11-2008, 03:32 AM -
[SOLVED] Simple Q: Values between Classes
By a45b22chp in forum New To JavaReplies: 6Last Post: 04-25-2008, 05:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks