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

java scope of objects.  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have been reading headfirst java and some other books practicing OOP and i have a question. can you use objects in classes they were not created in.

Basically i created a java tick tack toe game not using OOP. So here is what i need. i have a launch class, startNewGame and a PlayerClass

i created a object in startNewGame calling PlayerClass how can i use value (String in this case) in the object in another class like Board Class.
Do i use a constructor or do i just use object.Method in the Board Class.
or do i have to create a new object in boardClass calling the method in PlayerClass.

PS playerClass so far gets a players name and returns it. should i also use it to get that players move or should i create another class.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,

In Java there are scopes defined by curly braces, an open curly brace "{" and a closing curly brace "}" object that are created in the curly braces go out of context when you leave the scope unless there is a reference variable from outside of the scope that the object is bound to:

If you need a reference to an instance of an object in a different object than it was created, then you can get the reference a few ways, the 2 most direct being passing the reference to a constructor of the new object of the setter in the new object.

The instance of MS created externally to Junk2 will be available for the life of Junk2 by calling getMyStuff(). The constructor pattern has to be met by passing a reference to a MyStuff object at creation. If it is not convenient to do in a constructor, then a setter can be used to set the reference appropriately.

If you create a MyStuff object in Junk, then create a MyStuff object in Junk2 those objects will be distinct, not the same. You can do that, but you are not using the same object. In the case of passing a reference to another object, then you are working with the same instance of the object.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going into pedantic mode: objects don't have scope. They have a lifetime (usually on the heap), but it is variables which have scope. When the last variable pointing to a particular object goes out of scope, the object becomes unreachable and may be subject to garbage collection.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I am going into pedantic mode: objects don't have scope. They have a lifetime (usually on the heap), but it is variables which have scope. When the last variable pointing to a particular object goes out of scope, the object becomes unreachable and may be subject to garbage collection.




Ok that makes sense, but still for example say I create a object and I do object.getSomething and that function returns something I can use it like output object.getSome Variable+ 5

Or output object.getSomeVariable.
Because the variable in the class is private so I use the set and get method to access it.

So What all can I do with object.getSomeVariable

Can I use it in anotherClass? Can I use it like a variable or do I put it in a variable then use it in a anotherClass or should I just create a different object in the anotherClass.

Because the object "pointing" to it is the object reference you create right? such as ClassOne object;
Then you create a new object: object = new ClassOne. Now object is my reference our pointer variable to what ever I'm trying to get from that class.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just pass the object to the other instance/class? ... either via method call/return, or via a shared reference (at a scope accessible to both)?

Henry
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Why not just pass the object to the other instance/class? ... either via method call/return, or via a shared reference (at a scope accessible to both)?

Henry


So like AnotherClass {
Object2 = new
object2.object.getSomething

We're object.getSomething was create in a different class and getSomething is getting something from a 3rd class

What do you mean a scope accessible to both?

I might be over thinking this but just making sure I know how it works and how I can use object.get Something().
what I would think you have to do is just create another object for the other Class that accesses the Class.

So ClassOne{ object = new ClassThree
Object.getSomething}

ClassTwo{object2 = new ClassThree
Object2.getSomething}

ClassThree{ setSomething() getSomething()

Like if I have a game class and a board class that access stuff in player class just create delegate objects for each.
But....object.getSomething() is just a reference so can I use it outside its created scope or aces it like you said our like object2.object.getSomething
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you learned method calls with arguments yet? If not, then perhaps it would be good to wait a bit. It is pretty straightforward to pass objects around.

Henry
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Have you learned method calls with arguments yet? If not, then perhaps it would be good to wait a bit. It is pretty straightforward to pass objects around.

Henry


Yes, arnt method calls just able to call and send stuff to other member method. I thought that was a main point of Using objects? So can I just pass objects like a variable to another class it was not created in, through a method call?

I feel like I should just do this.
ClassOne{object new object ClassThree
Object.getSomething()
}

ClassTwo{object2 = new ClassThree
Object2.getSomething()}

ClassThree{setSomething()
getSomething()
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
I feel like I should just do this.


Perhaps it would be a good idea to use real code -- instead of pseudo code. As it is somewhat difficult to understanding what you are trying to say.

Henry
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us imagine you have a Kettle pointed to by the variable k, containing 10 of water at a temperature of 75.Now, you can assign those values to other variablesNow you can pass those variables to methods or constructors

Those 1‑letter variable names are poor style.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//? Can I // System.printnl(object.getStringNana);
//or put object.getStringName into a variable or constructor to pas to ClassOne

//or just create a new object

If I understand you correctly -- no, you can't access the variable object from ClassOne, you need to create a new object.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean, it seems to me you're trying to generalize too soon. In other words, yeah, I think you're overthinking.

Start with specific examples. Object orientation is about behaviors and relationships. Properties/attributes are just things that objects use to help determine how they will behave in response to some stimulus and/or in relation to other objects. There are many different ways to write programs around these general concepts.

If you don't want to get overwhelmed, start with specific examples and see how the programs would best be written using OOP concepts. Then try it with another specific example. Then another. When you've done this hundreds or thousands of times (yes, literally) then you'll start seeing the patterns and can make appropriate decisions as to how to design things properly. This is where the intuition that experienced programmers have comes from. Until you have reached that level, go easy on yourself.

Now, what specific problem are you struggling with regarding your program? What value of a Player object did you want to use in the Board object? The player's name? What does your code look like? Show it to us. On the surface, what you describe seems like a plausible approach but I don't think anyone can comment with certainty until they've seen your actual code. Should you have the Player be responsible for getting user input for a Move? Again, it's kind of hard to comment on that until we've seen your code. And please be kind, just post enough code and format it properly, with code tags.

Without any concrete code to talk about, this conversation will progress very slowly with people trying to guess what you're writing and deduce what you're thinking. We're programmers and we're pretty good with logical reasoning and problem solving, but we're not psychics .
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, what you gave is not a good concrete example.
Sean Paulson wrote:

It gives us no context as to what problem you're trying to solve or how the behavior of each of these classes are related to each other. You could just as well written this:

None of that makes sense, right? If there's no reasonable context that we can relate to here, then how can you expect to get a sensible answer?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,

A good analogy that I have seen recently is this:

Let's liken object to people--and references to cell phones. Do you have Tina L's number, if you do you can call Tina L and talk to her. Bob can also call Tina L, and talk to her, she may even have a group chat line and everyone that has Tina L's number can chat with her. During your chat, you ask Tina L to do something, and if she can she will. Tina L will share anything she considers public with you or even things which may be of a more restricted nature provided you have the right access. You also have a reference to Tina P. If you call Tina P, she doesn't know Tina L, so she cannot help you with thing Tina L knows. Obviously Tina P may have some of the same things as Tina L, but most likely all the things of Tina P are distinct items from anything Tina L has, so if Tina P gives you her favorite box of cookies, Tina L's cookie supply is not affected.

Les

Sean Paulson wrote:
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:A good analogy that I have seen recently is this:

Let's liken object to people--and references to cell phones. ... so if Tina P gives you her favorite box of cookies, Tina L's cookie supply is not affected.

That started out really good but somehow I got lost along the way. By the time you got to the cookies, I was a little bit
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you can have phone numbers to two girls with the same names, but they are not the same girl. Even though they have the same name, and say both of them have a box of cookies, girl1's cookies are distinct from girl2's cookies as are the girls themselves--distinct, but of the same name. You might say they were made in different scopes--perhaps girl1 was born to parents in New Zealand, and girl2 was born to parents in Germany. Or even if they were both born for the same parents--twins even--both girls are distinct, separate from each other. When you create objects, generally speaking, the same is true.

even though both objects where made from the same class, in the same scope and at nearly the same time, they are distinct; separate objects not dependent on each other.

But if you do this:


You have 2 references, ms1 and ms2, that reference the same object. In the analogy I was using, it would be like Tina L has 2 cell phone numbers--both numbers call Tina L.

Junilu Lacar wrote:
Les Morgan wrote:A good analogy that I have seen recently is this:

Let's liken object to people--and references to cell phones. ... so if Tina P gives you her favorite box of cookies, Tina L's cookie supply is not affected.

That started out really good but somehow I got lost along the way. By the time you got to the cookies, I was a little bit
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Basically, you can have phone numbers to two girls with the same names, ...
Junilu Lacar wrote:That started out really good but somehow I got lost along the way. By the time you got to the cookies, I was a little bit

My tongue was firmly planted in my cheek when I posted that reply, just in case you didn't notice. Still not sure how it got to the cookies all of a sudden.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!