Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 02:44:07 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)(Java) Dependency Injection?
Pages: [1]
Print
Author Topic: (Java) Dependency Injection?  (Read 664 times)
eliasfrost
Level 0
***



View Profile
« on: July 23, 2015, 04:20:34 AM »

Hi! I'm currently learning a bit of Java on my freetime and I have stumbled upon a problem: I want to access an object that was created in another class. I've done a little bit of research and I stumbled upon a term called dependency injection but I'm having trouble working out how it works in practice as most examples has been very vague, here's hoping someone can explain to me in a bit more noob-friendly way how to achieve my goals. Thanks in advance! Smiley

Here's a simplified version of my code (so that there's minimum clutter):

Code:
class MainApp{
  public static void main(String[] args){
    Player plr = new Player();
    Stats window = new Stats();
  }
}

class Player{
  int a;
  public int getA(){
    return a;
  }
}

class Stats extends JFrame{
  // JFrame window stuff
  public void dosmth()
  {
    plr.getA(); // <-- What do I do to allow me to access the object created in MainApp?
  }
}
« Last Edit: July 23, 2015, 06:07:42 AM by eliasfrost » Logged

dancing_dead
Level 2
**


View Profile
« Reply #1 on: July 23, 2015, 05:53:04 AM »

plr is out of scope in Stats class, you need to have a reference to the player, before you can call methods on it. you also can't call methods like that in class's body, you need to wrap it in a method itself. something like:

Code:
class Stats extends BlahBlah {

    private Player plr;

    public Stats(Player plr) {
        this.plr = plr;
    }

    public void doSmth() {
        plr.getA();
    }
}


and then in your main():

Code:
public static void main(blah blah) {
    Player plr = new Player();
    Stats window = new Stats(plr); // <- we pass a reference to the object to stats'constructor, so stats know what plr we talking about
    window.doStuff(); // this now finally does plr.getA();
}

this is real beginner stuff, I recommend getting a book and following through that, or else it might be too confusing too fast.
Logged
eliasfrost
Level 0
***



View Profile
« Reply #2 on: July 23, 2015, 07:09:38 AM »

Quote
You also can't call methods like that in class's body, you need to wrap it in a method itself. something like:
Whoops, that was a typo on my part!

Thanks for the answer!

Here's how I understand it, please tell me if I'm wrong:
-I create the Player object in class MainApp
-In the Stats class I create a private Player object
-In the constructor for the Stats class I pass the reference of the Player Object into the private Player object with this.plr = plr;
-Now I can use the private Player object in the Stats class as if it was the original object (even though it isn't, it's just a copy)

correct?

Yeah I really should get a book, following different tutorials from all over the place have proven to be quite a hassle, is there one in particular you can recommend?
Logged

dancing_dead
Level 2
**


View Profile
« Reply #3 on: July 23, 2015, 07:32:18 AM »

no, no, no, that's not how it works.

in your Stats class, where you write:

Code:
private Player plr;

you simply declare a member field with a name and a type, but you do not create any new objects here! the only time in java when you create new objects, is when you call "new", like in your main (that is part of MainApp), where you create

Code:
Player plr = new Player(); // <- notice the "new" here, this and only this creates an object

then when you assign player in Stats() constructor (constructor's called that, because it's the method that constructs a new object) to the member field this.plr, you do not copy the whole player object, but you merely make the private member field "point" to the same object that was passed in as an argument. or, to be more precise, what you pass in as argument is not the object itself, but a reference to that object. so the constructor:

Code:
public Stats(Player plr) {

    this.plr = plr; // <- this copies the value of the argument, which in this case is a reference to an object! this DOES NOT copy the object itself!!!!

}

merely copies the reference to the object, but does not copy the object itself. there's still only one player object around, but it's referred to now in two different places. if you make a modification to player from one place (such as setting plr.A = 5), then getA() called from the other reference will also now return 5.

the only things that behave like you thought they do are primitives in java. there's only a few of them, boolean, int, float, etc. all these primitive types do indeed get copied around, but they have special status in java, absolutely all other objects are accessed via references, and any assignments done with objects merely copy the value of the reference, but not the object.

you should probably google for good introductory java book, I don't know any off my head, but I'm sure stackoverflow has a few answers lying around.

edit: an exception to the "new" rule is strings of various kinds. all strings in Java are objects, not primitives, but they do not need a "new" to be created, a string literal is enough. so, when you type:

Code:
String myFancyString = "Look, how fancy I am!";

you DO create a new object, to which the variable myFancyString refers to. thought this was worth mentioning.
« Last Edit: July 23, 2015, 07:41:25 AM by dancing_dead » Logged
eliasfrost
Level 0
***



View Profile
« Reply #4 on: July 23, 2015, 07:51:05 AM »

So the private Player plr; is essentially null? And when I put = new Player(); after it, that's when I give the field something to point to (i.e. a new object)?

So this.plr = plr; is making plr point at the Player object passed through the constructor argument?


Sorry, I need to put things in my own words to more easily understand what's happening. Correct me if I'm wrong once again. Tongue
Logged

Dacke
Level 10
*****



View Profile
« Reply #5 on: July 23, 2015, 07:57:22 AM »

You got it.

private Player plr; is indeed initialized to null, unless you set it to something.
Logged

programming • free software
animal liberation • veganism
anarcho-communism • intersectionality • feminism
eliasfrost
Level 0
***



View Profile
« Reply #6 on: July 23, 2015, 08:27:26 AM »

Thank you guys for the help, it's very much appreciated! Smiley
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic