Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to communicate between classes?  RSS feed

 
Johnny Fawkes
Greenhorn
Posts: 4
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I would like to know how can I change a value inside another class, without creating a new object?
Imagine this situation:
You have 3 classes:
  • main.class
  • door.class
  • window.class


  • door.class has a field: String color;
    window.class has a similar field: String shape;

    When the program runs, main.class creates both objects:



    Question: How can I change d1.color = "somethingElse" from window.class without making another door object?
    Or more accurately: Is it possible to change an existing object's properties from other .class files?

    It would be nice if you could point me towards a tutorial that covers this problem.

    Thank you
     
    Deepak Chopra
    Ranch Hand
    Posts: 433
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you want to change a class property rather than object property.

    in order to tie any property to a class - you need to define it "static'.
    In order to make it accessible to other class - you need to make it scope public, protected or default.

    for example -
     
    Greg Brannon
    Bartender
    Posts: 563
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Johnny Fawkes wrote:Hello,
    I would like to know how can I change a value inside another class, without creating a new object?


    I think your question is not correctly represented by your example, and your pseudo code is confusing in its variation from commonly-used Java. Can you describe in plain English what you're trying to do?

    Changing the values of an instantiation of a class from inside another class is commonly and easily done, and it doesn't require creating another instantiation of that class - in fact, that would be pointless. So, if you can explain what you're trying to do a little more clearly, we can give you a good answer.

     
    Johnny Fawkes
    Greenhorn
    Posts: 4
    Chrome Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Greg Brannon wrote:

    Changing the values of an instantiation of a class from inside another class is commonly and easily done, and it doesn't require creating another instantiation of that class



    Yes! That is what I am trying to understand, how can I change the values of an instantiation from another class?
     
    Greg Brannon
    Bartender
    Posts: 563
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Use Java accessor and mutator methods, also known as getters and setters. Accessor methods are used to return the object's state, mutators to change the object's state. The accoessor and mutator methods belong to the same class as the object you desire to read or manipulate, so are invoked like:



    I tried to find a good tutorial for you to review but didn't like what I found. Try looking on your own, or someone else here may be able to pull up a good tutorial. I'll keep looking when I have more time and let you know if I find something.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i'd look at the api for the door class. perhaps there is a method called 'changeDoorColor', and then I'd use that.

    Door d1 = new Door("red");
    d1.changeDoorColor("blue");

    It can be as easy as that - but it depends on the Door class. If you own that code, you can write it. If you don't, you have to hope the person who did left you a way to change it.
     
    Johnny Fawkes
    Greenhorn
    Posts: 4
    Chrome Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Greg Brannon wrote:Use Java accessor and mutator methods, also known as getters and setters. Accessor methods are used to return the object's state, mutators to change the object's state. The accoessor and mutator methods belong to the same class as the object you desire to read or manipulate, so are invoked like:



    I tried to find a good tutorial for you to review but didn't like what I found. Try looking on your own, or someone else here may be able to pull up a good tutorial. I'll keep looking when I have more time and let you know if I find something.


    Okay, thanks, I would have searched for a tutorial myself, but I didn't know what to look for.
    -Issue resolved.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!