You're probably just wanting something like this:
player.java:
public class Player
{
public static void main(String[] args) {
Player player = new Player();
Monster monster = new Monster();
monster.attackPlayer(player);
}
public int getLocation()
{
return 2;
}
}
monster.java:
public class Monster
{
public void attackPlayer(Player player)
{
player.getLocation();
}
}
Hope that helps/makes sense!
Answer from awolfe91 on Stack OverflowYou're probably just wanting something like this:
player.java:
public class Player
{
public static void main(String[] args) {
Player player = new Player();
Monster monster = new Monster();
monster.attackPlayer(player);
}
public int getLocation()
{
return 2;
}
}
monster.java:
public class Monster
{
public void attackPlayer(Player player)
{
player.getLocation();
}
}
Hope that helps/makes sense!
A good place to start would be to pass in the player you want to attack "when" the monster attacks.
battle.java
public class Battle {
public static void main(String[] args) {
Player player = new Player();
Monster monster = new Monster();
monster.attackPlayer(player);
}
}
player.java:
public class Player
{
public int getLocation()
{
return 2;
}
}
monster.java:
public class Monster
{
public void attackPlayer(Player player)
{
player.getLocation();
}
}
[JAVA] How to use an object from another class
How do I access an object from another class in java? - Stack Overflow
java - Access Objects from another class? - Stack Overflow
How can access a object in main class from another class in java? - Stack Overflow
I'm fairly new to Java programming, and I've hit a bit of a wall trying to work out how to use two variables called from another .java file.
public static double distance(DataObject object1, DataObject object2)
DataObject.java is another file, and I'm supposed to be using object1 and object2 to determine the distance between the two. I'm all good with working out the math behind doing so, but I have no idea how to actually USE object1 and object2. Just slapping double sum = object1 - object2 sends JAVA into a tiffy.
Can anyone give me an idea of how to use these two variables? Right now I'm simply doing
if ((((object1 - object2)2)Math.sqrt)) { return true; } else { return false; }
edit: I should have mentioned that this is for an assignment. I'm not sure if copying/pasting the entire code would be a problem in their eyes; plagiarism etc. I'll suss that out tomorrow. If anyone has any decent links to some good, beginners Java sites that might give me a good idea I'd appreciate it! I've scoured online and slowly getting some idea of how to do it.
Well, one thing is you can't run some arbitrary line of code (System.out.println) in a class definition, so your example is invalid.
obj is an "instance variable" in Class1 — Class 2 would need to have a reference to an object of Class1 in order to access the "obj" member of that instance.
// in Classx.java
public class Classx {
}
// in Class1.java
public class Class1 {
Classx obj = new Classx();
}
// in Class2.java
public class Class2 {
Class1 anObj = new Class1();
public void showIt() {
System.out.println(anObj.obj);
}
}
// in TestProg.java
public class TestProg {
public static void main(String... args) {
Class2 two = new Class2();
two.showIt();
}
}
Since obj in Class1 is not declared public, indeed no modifier is given, it has the default access which is package, so Class1 and Class2 would need to be in the same package to allow this use of anObj.obj.
Private Objects
public class Class1
{
//...
Classx obj = new Classx();
//...
public Classx getClassx()
{
return obj;
}
}
and
public class Class2
{
//...
Class1 c1 = new Class1();
System.out.println(c1.getClassx().property);
}
Public Objects
public class Class1
{
public Classx obj = new Classx();
//...
}
and
public class Class2
{
//...
Class1 c1 = new Class1();
System.out.println(c1.obj.property);
}
Static public Objects
public class Class1
{
//...
public static Classx obj = new Classx();
//...
}
and
public class Class2
{
System.out.println(Class1.obj.property);
}
Some hints or advice about your code that help you :
1. try to define your setter and getters for encapsulation purposes ,and having access to each field.
for example:
private int ID;
getter method is
public int getID(){
return this.ID;
}
setter method is
public void setID(int newID){
this.ID = newID;
}
2. Instead of having displayMinionData() , it is good practice to override toString method
For example if your variables are :
private String came;
private String city;
public String toString(){
return name + " " + city;
}
I am not sure what you by accessing the solider in main method:
but you have this
Minion soldier = new Minion(0, "Soldier", "Human", "The First minion", 2, 1, 1);
As I told you
1. you can printout the whole info by using overridden toString on the console
2. you can extract each portion of the soldier variable by using getters
3. you can update each portion of the soldier variable by using setters
sample for toString:
System.out.print("Info about the current soldier is " + soldier.toString());
sample for getters:
System.out.println("ID:" + soldier .getID()):
sample for setters:
int newID = 123456789;
soldier.setID(newID);
System.out.println("The new id of current soldier is " + soldier.getID()):
If you want to access individual fields (such as name, health, attack, etc) you will have to add public access for them. By declaring them private, you made them only visible inside the Minion class. That's not a bad thing! It's best practice to keep fields private, but provide public getters / setters to get or set the values, respectively. This ensures they can't be changed except by specific methods that you have written.
To write a getter/setters, make two methods for each field which needs public visibility:
public int getID(){
return this.ID;
}
public void setID(int ID){
this.ID = ID;
}
Then, you can create a Minion (as you have) and do something like System.out.println(soldier.getName());
(By the way, it's convention in Java to capitalize classes (like Minion and MinionTracker) and camelCase objects (like soldier or minionName)
I believe the option that follows Object-Oriented Programming principles best is to pass the actual Player in as an argument, instead of just the money.
Basically, passing just the player's money in instead of the player themselves is like just handing your wallet over to the cashier. You wouldn't do that, right?
This way, the clerk can ask the customer if they have enough money by calling player.getMoney(), and the customer can tell them the answer.
After making the purchase, the player can remove the money from their wallet themselves when the clerk asks them to via player.setMoney().
Now, you asked in a comment about "passing the actual player as an argument without creating a new object of the player class." Java passes arguments by value, and all objects' values are simply the address that hold the information for that particular instance.
So for Player p1 in Beta, let's pretend all of p1's information is stored in a block starting at...let's say, address 21343. In this case, the variable p1 only contains that single number, 21343.
Since Java passes by value, then when you call s1.clerk(Player player), the player variable will also contain 21343. Since it's editing the items contained at the same address, you've essentially passed on p1 itself instead of creating a new Player. In short, the clerk and the main method work with the same object.
The fact that Java passes by value is also why passing just the player's money in doesn't adjust it automatically: The money is an int rather than an object. Since it's an int, when you pass it to the clerk, you're just saying "Hey, clerk, this is the amount of money being worked with." But the clerk has no idea who the money belongs to, so while it can take money, or even give it some, it's essentially just setting it down on the counter, and it's the responsibility of the player to pick it up from there when they're done. By passing in the player instead, the clerk knows who the money belongs to because it's actually asking the player how much money they have.
Another potential solution would be to make p1 and s1 static variables in the Beta class. It'd look something like this:
public class Player
{
public static Player p1;
public static Shop s1;
public static void main(String[] args)
{
p1 = new Player("Test");
s1 = new Shop();
p1.setMoney(100);
s1.clerk(p1.getMoney());
}
}
From there, you'd import the Beta class in Shop, then call Beta.p1 in Shop to access p1.
Hope this helps!
While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically. This example:
public class MyClass {
public static String xThing;
private static void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
private static void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
makeThing();
makeOtherThing();
}
}
Will work, however, it would be better if it was more like this...
public class MyClass {
private String xThing;
public void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
public void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
MyClass myObject = new MyClass();
myObject.makeThing();
myObject.makeOtherThing();
}
}
You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.
public class SomeClass {
NumberList numberlist; // Definition
....
Then in your create() function just say:
numberlist= new NumberList(length, offset); // Initialization