0

I have an object declared in one class but now I am in another class and I want to get the field of the object without making everything static. In the current class I don't have the object which exists in the other class and I don't really understand how to call it. If i use a getter I guess I still need the object in the current class to call it, also I don't want to make the fields static, because I want the field unique for every object.

package main; import daysOfTheWeek.*; public class Game { private int currentDayOfWeek = 1; private int totalDays = 1; boolean playerHadEvent = false; public void startGame(){ while(true){ boolean eventDay = false; if(currentDayOfWeek == 5 || currentDayOfWeek == 6){ //check if current day isn't Friday or Saturday eventDay = true; } System.out.println("==DAY " + totalDays + ", " + DaysOfTheWeek.getDay(currentDayOfWeek)+"=="); if(eventDay){ String event = Events.getEvent(); if(event.equals("gaming")){ System.out.println("Today there is " + Messages.getGamingEvent()); if(){ //get the field from another class's object } } } totalDays++; currentDayOfWeek++; if(currentDayOfWeek == 8){ currentDayOfWeek = 1; playerHadEvent = false; } } } } package main; import daysOfTheWeek.*; import dices.*; public class Demo { public static void main(String[] args) { Student player = new Student(); Student computer = new Student(); player.generatePlayer(player); computer.generateComputer(computer); Game startingGame = new Game(); startingGame.startGame(); } } 

thats how i created the 2 objects and set their fields trough a method. Now i am trying to use those 2 objects but in 3rd class

4
  • Sorry, I don't understand your question at all. How is the code related to the question? Can you make references to specific parts of your code in the text of your question? Also, what do you mean by "I don't have the object which exists in the other class and I don't really understand how to call it"? You can't call an object; you can only call methods. Commented Jan 19, 2018 at 11:12
  • I have an object declared in other class, but I cant use it in this class, maybe I should try call it trough method so i can use the objects fields in this class ? Really sorry but this is my first OOP program and I am having hard time. Its even more complicated because I declared it in one class and set its fields trough a method from his original class, now I want to use this object in a 3rd class Commented Jan 19, 2018 at 11:16
  • 1
    You need an instance of the object. Commented Jan 19, 2018 at 11:16
  • You still actually haven't shown your problem. One two or three classes it really doesn't matter. You have have an instance of class A, and it needs a reference to an instance of class B. You can either explicitly pass a reference to an instance of A to your instance of class B (ie through a setter or constructor) or you can create a reference to your class B as a static member (this is not good practice, unless there is a reason.) Commented Jan 19, 2018 at 11:36

1 Answer 1

1

In your main class:

public class Main{ public static void main(String[] args){ Game game = new Game(); System.out.println(game.getCurrentDayOfWeek()); } } 

So you need to create the method getCurrentDayOfWeek.

You could make a singleton type pattern.

class Junk{ int value; static Junk instance = new Junk(); static int getJunkStuff(){ return instance.value; } static void setInstance(Junk j){ instance = j; } } 

It sounds like you should structure your code better, so maybe you should paste a more complete example.

A third class needs a reference to those objects? Then give it a reference.

class Third{ Game game; public Third(Game g){ game = g; } public Third(){ } public void setGame(Game g){ this.game = g; } } 

So the third example, lets Third have a reference through a setter.

Sign up to request clarification or add additional context in comments.

6 Comments

No sorry maybe my question isn't clear enough. This is working fine. I have a static method and I call it trough the class' name. I just need reference to an object i already have in other class and can't figure how to make it
Your question is not clear at all. What does your question have to do with your code?
@GeorgiMichev I updated the answer to include another example. Maybe that is more in line with what you are trying.
@GeorgiMichev put it in your question, formatted, not a comment.
Yes I also added the other class here. Now I just need the references to those same two objects in my 3rd class, so i can access their fields
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.