DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page Getting values of another class
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Active Contributor
 
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default Getting values of another class - 01-30-2008, 03:46 PM

Once again, I've got a question. It's really simple but I don't know the right/best code to do it.
I want to get the values of some variables of other classes. What's the best way to do that? Do I have to make those other classes public?
Here's the code:

Code:
class Teacher{
	int severity;			/* on a scale from 1-10*/
	int enthusiasm;			/* on a scale from 1-10*/
	String mood;			
	String wayOfTeaching;
	String health;			/* possible values: healthy or sick*/
	String subject;
	String sex;
}

class TeacherFrench extends Teacher {

	TeacherFrench() {
		this.severity = 7;
		this.enthusiasm = 8;
		this.mood = "Good";
		this.wayOfTeaching = "Writing everything on the blackboard.";
		this.health = "Healthy";
		this.subject = "French";
		this.sex = "Male";

	}

	public static void main (String[] args) {
		TeacherFrench misterBackaert = new TeacherFrench();
		System.out.println("Severity: " + misterBackaert.severity + "\n" + "Enthusiasm: " + misterBackaert.enthusiasm + "\n" + "Mood: " + misterBackaert.mood + "\n" + "Way Of Teaching: " + misterBackaert.wayOfTeaching + "\n" + "Health: " + misterBackaert.health + "\n" + "Subject: " + misterBackaert.subject + "\n" + "Sex: " + misterBackaert.sex);
	}
}

class Student{
	int laziness;			/* on a scale from 1-10*/
	int enthusiasm;			/* on a scale from 1-10*/
	int tired;			/* on a scale from 1-10*/
	int bored = 0;			/* on a scale from 1-10, always starting with 0, increasing while the lesson goes on*/
	int brainCapacity;		/* on a scale from 1-10*/
	Boolean chewingGum;		/* if true, he has a chewing-gum, if false, not*/
	String mood;			/* possible values: disaster, bad, neutral, good, excellent */
	String health;			/* possible values: healthy or sick*/
	String sex;			
}

class StudentDieter extends Student {
	
	StudentDieter () {
		laziness = 8;
		enthusiasm = 7;
		tired = 6;
		brainCapacity = 6;
		mood = "good";
		health = "healthy";
		chewingGum = true;
		sex = "male";
	}
	
	public static void main (String[] args) {
		StudentDieter dieter = new StudentDieter();
		System.out.println("Laziness: " + dieter.laziness + "\n" + "Enthusiasm: " + dieter.enthusiasm + "\n" + "Tired: " + dieter.tired + "\n" + "Braincapacity: " + dieter.brainCapacity + "\n" + "Mood: " + dieter.mood + "\n" + "Health: " + dieter.health + "\n" + "Chewing-gum: " + dieter.chewingGum + "\n" + "Sex: " + dieter.sex);
	}
}

class ClassSituation {
	public static void main (String[] args) {
		/*Call for the values of StudentDieter and TeacherFrench here to make some calculations*/
	}
}
So how can I get the values of the int's and String I defined in TeacherFrench and StudentDieter to make some calculations in ClassSituation?
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Jan 2008
Location: Germany, Munich
Send a message via MSN to mende
Default 01-30-2008, 05:35 PM

At first it would be a good idea to make the classes public.
The other point is that you should use accessors (the getters and setters) to access the fields of a class (int laziness for example).

Code:
public class Teacher{
	int severity;			/* on a scale from 1-10*/
	int enthusiasm;			/* on a scale from 1-10*/
	String mood;			
	String wayOfTeaching;
	String health;			/* possible values: healthy or sick*/
	String subject;
	String sex;

  public int getSeverity() {
    return this.severity;
  }

  public void setSeverity(int severity) {
    this.severity = severity;
  }

  ... (for all fields which you want to make accessible) ...
}
By the way, it would be a good idea to make the fields private, currently they're package-public, that means that other classes from the same package can access them which is normally not wanted.

Now, if you've got these accessors and the classes public you can instantiate both classes in you ClassSituation.

Code:
public class ClassSituation {
	public static void main (String[] args) {
		StudentDieter sd = new StudentDieter();
                TeacherFrench tf = new TeacherFrench();
                int maxEnthusiasm = sd.getEnthusiasm() + tf.getEnthusiasm();
                // do whatever you want with the maxEnthusiasm.
	}
}
I haven't tested the examples but I think they should work (in theory).

Marc


Ceterum censeo Carthaginem esse delendam.
Reply With Quote
  (#3 (permalink)) Old
Active Contributor
 
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 01-31-2008, 03:06 PM

I have to put the differents classes in a different document if I make the classes public isn't it? So is it really necassary to make it public? I guess not.
What happens if I have two variables with the same name in two different classes and I make the accessors of them? Will it give an error.

Last edited by svds; 01-31-2008 at 03:25 PM.
Reply With Quote
  (#4 (permalink)) Old
Active Contributor
 
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 01-31-2008, 04:08 PM

When I don't use the accesors, so when I simply don't put them into the source code, I can still print out the values of the variables in my class ClassSituation by calling them with the objectname.variable
So is it really necessary to use the accesors or is it more common or is there simply a problem with my code that makes this possible?

E.G.:
Code:
class TeacherFrench extends Teacher {

	TeacherFrench() {
		this.severity = 7;
		this.enthusiasm = 8;
		this.mood = "Good";
		this.wayOfTeaching = "Writing everything on the blackboard.";
		this.health = "Healthy";
		this.subject = "French";
		this.sex = "Male";

	}
}

class ClassSituation {
	public static void main (String[] args) {
		TeacherFrench misterBackaert = new TeacherFrench();
		System.out.println("Severity: " + misterBackaert.severity);
             }
}
And another (little) thing. When I make my integers/Strings private I can't use them in the inherited class. I can't define them in my contructor of the inherited class. At least not with the keyword this. Is there another possibility to define them using private variables?

Last edited by svds; 01-31-2008 at 04:22 PM.
Reply With Quote
  (#5 (permalink)) Old
Moderator
 
gdboling's Avatar
 
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 02-01-2008, 01:09 PM

Well, what you are dealing with here is some basic Java 101 type stuff. I don't mean that in a bad way. But these are common object oriented ideals that need to be learned. So I'll try and run through a brief tutorial.

Scope - Variables in Java have scope depending on their scope declaration and when/how they are used.
  • Public - Visible to all classes. In all honesty you probably won't use this scope as much as you might think
  • Protected - visible to classes outside the package that inherit the class, also to all classes in the package.
  • Private - visible only within the class, not by inheritors, not by other classes in the package.
  • Default - visible to all classes of the package.

There is also a scope considered 'local' which is a variable defined within a block of code, like a method. These local variables are only visible to that block of code.

The standard practice when it comes to JavaBean type classes is to have all private variables with accessor methods (getters/setters) as mende suggested. This could be debated but 99.9% of the time it is accepted practice in the Java world.

Code:
public class Teacher{
	private int severity;			/* on a scale from 1-10*/
	private int enthusiasm;			/* on a scale from 1-10*/
	private String mood;			
	private String wayOfTeaching;
	private String health;			/* possible values: healthy or sick*/
	private String subject;
	private String sex;

        // provide getters and setters for all private variables
}
Code:
class TeacherFrench extends Teacher {

	TeacherFrench() {
                setSeverity(7);
                setEnthusiasm(8);
                setMood("Good");
                setWayOfTeaching("Writing everything on the blackboard.");
                setHealth("Healthy");
                setSubject("French");
                setSex("Male");
	}
}
Code:
class ClassSituation {
	public static void main (String[] args) {
		TeacherFrench misterBackaert = new TeacherFrench();
		System.out.println("Severity: " + misterBackaert.getSeverity());
             }
}
You could also provide a constructor in Teacher that accepted values for all variables and when a new class of type Teacher is created you could just make the call to the constructor via super(). But that can be done after you understands the basics above.

Did that help at all?


Thanks

Gregg Bolinger
Reply With Quote
  (#6 (permalink)) Old
Member
 
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Feb 2008
Post Java Game Development - 02-09-2008, 12:56 PM

Ah... wrong button...??

Last edited by Kevinul; 02-09-2008 at 12:57 PM. Reason: Delete Post, please...
Reply With Quote
  (#7 (permalink)) Old
Active Contributor
 
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Default 02-12-2008, 02:31 PM

Thanks, it helped a lot. And I don't see it as an insult, I see it as a chance to learn thongs. That's why I post my questions.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building to chroot OR how to remove "built-in values" zmalex Eclipse 1 03-19-2009 08:54 PM
Execute/debug given class from a project. DanielKec NetBeans 0 01-16-2009 08:44 AM
Override a 3rd party plugin class - RCP SatishP Eclipse 0 11-25-2008 05:18 AM
Accessing properties file keys/values sahuabinash Java 0 04-10-2008 09:39 AM
suggest the wizard class to use ?? jayjamba Eclipse 0 04-10-2008 12:48 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com