Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Björn Norén
9,569 PointsHangman | Help with the concept of obejcts as arguments
I find it hard to understand how it works when we pass "game" as an argument when we create a new "Prompter-object".
I've really tried to follow the code, but I felt I needed to try with something simplier.
Let's say that we have a very simple calculator-program with 3 classes, which only does multiplication.
- Main.java
- Variables.java
- Multiplication.java
How do I get to the variables in the "Variables-class" so I can use the multiplication-method in "Multiplication.java"? Any help is appreciated, but I would be superglad if someone could explain what it really means to create a object as an parameter and how we then work with it. Because I do understand the purpose of seperating the logic and practise, I just don't get how to achieve anything. Thanks!
* Variables.java
public class Variables {
public int x;
public int y;
// This constuctor sets the variables when it gets created
public Variables (int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
}
}
*Multiplication.java
public class Multiplication {
public Variables variables;
public Multiplication (Variables variables) {
this.variables = variables;
}
// The idea is that this method should use the varaibles in the other class to multiplicate
public void multiplicate () {
System.out.println(x * y);
}
public static void main(String[] args) {
}
}
-
Main.java
public class Main { public static void main(String[] args) { // Here we sets the variables with the constructor, Variables variables = new Variables(5, 5); // Here we use the object as an parameter Multiplication multiplication = new Multiplication(variables); // So, how do I get to the variables x & y in "Variables.java"? multiplication.multiplicate(); }
}
1 Answer
Matthew Francis
6,967 PointsFirst off,
public static void main(String[] args) {
}
You do not need another main method class.
Secondly,
public void multiplicate () {
System.out.println(x * y);
}
You are saying that "what is x and y in the Multiplication class?", there is no x and y in the class because you did not create any. The variables from the Variable.class is not transferred to the Multiplication class, but since you did,
public Multiplication (Variables variables) {
this.variables = variables;
}
you can ACCES the variables inside Variable.class.
So how do you access the variables in Variable.class? like this:
void multiplicate () {
System.out.println(variables.x * variables.y);
}
This code is saying "Acces x inside variables class". This is called dot notation. I believe you have covered this in your previous course, but if you forgot, the dot notation is used to denote the hierarchy of the class.
Let me know if you have any more questions :)
Björn Norén
9,569 PointsBjörn Norén
9,569 PointsHuuuge thanks! It was exactly what I wanted to know, now I understand it a little bit more :)