Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.
Need Java help!?
Okay, what happens if I create an object within a class, and when I make an instance of that class, I can only reference the object through the other object? Here's an example.
class SomeObject {
int value = 99;
}
class Test {
SomeObject newObject = new SomeObject();
}
class game {
public static void main(String[] arguments) {
Test test1 = new Test();
System.out.println(test1.newObject.value);
}
}
Notice the "System.out.println" statement. I have to reference the "value" integer through "newObject," or else it doesn't work. If I just try to do newObject.value, it gives me an error. Why do I have to reference through that object and what did I just program here? Is there some kind of technique I just used that I unwillingly stumbles upon and is it an okay practice to initiate objects within classes? Thanks.
Well, I know how to reference "value," I just have to go through another object. I was just wondering what I did (I understand how and why it works) and if it's a good programming practice or not. I'm just messing around with Java, it's nothing serious or anything. I'm just testing out some things I've always wanted to test.
3 Antworten
- vor 1 JahrzehntBeste Antwort
OK if you want to know if this is good programming practice:
From my experience i will tell you some thing :
1- There is no problem to use object in side object
test1.newObject
2- you should not allow accessing variable from outside the class like this:
test1.newObject.value
because this violate OO concepts 'encapsulation' which means that you access the class object only by calling methods
How do you do that?
* Keep instance variables protected (with an access modifier, often private).
* Make public accessor methods, and force calling code to use those methods
rather than directly accessing the instance variable.
* For the methods, use the JavaBeans naming convention of
set<someProperty> and get<someProperty>.
- E.M.BedLv 5vor 1 Jahrzehnt
This is best illustrated by example.
So now try the following:
class SomeObject {
int value = 99;
}
class Test {
SomeObject newObject = new SomeObject();
}
class game {
public static void main(String[] arguments) {
Test test1 = new Test();
Test test2 = new Test();
System.out.println( test1.newObject.value);
System.out.println( test2.newObject.value);
}
}
Now we've got 2 instances of Test each with their own separate instance of SomeObject. We have to access the internal value separately to distinguish them.
Now this will make things clearer hopefully:
class SomeObject {
int value=0; //default
SomeObject (int val) { // constructor
this.value = val;
}
}
class Test {
SomeObject newObject;
Test (int val) { // constructor
this.newObject = new SomeObject(val);
}
}
public class game {
public static void main(String[] arguments) {
Test test1 = new Test(16);
Test test2 = new Test(23);
System.out.println( test1.newObject.value);
System.out.println( test2.newObject.value);
}
}
Output is
16
23
We have added constructors for both Test and SomeObject to initialise the value variable. You can see from the output that there is a different instance of SomeObject in Test1 and Test2 which is why you need to explicitly reference the object you require to avoid ambiguity..
If you just did this:
class SomeObject {
int value = 99;
}
public class game {
public static void main(String[] arguments) {
SomeObject newObject = new SomeObject();
System.out.println( newObject.value);
}
}
There is only a single instance of SomeObject contained within the game class so can be referenced directly within the game class as opposed to another class.
Note game should be public in a file game.java.
- vor 1 Jahrzehnt
make value a static variable....(note all objects refer to same value now).......
it can b now accessed Test.value=123;