Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to keep an object in scope  RSS feed

 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I called a method to set the hour and store it in a files in the object called recardObj, but when i go and pull try and print the stored value from the field I get back 0.
Can anyone tell me how to keep this object in scope.












 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are setting the hour on one object, and getting it on another object. This is not a scoping issue. Your currentHour() method should return the object on which you have set the hour, so that the main method can get the hour from that object.

Another thing, if you're working with time, please don't use Calendar, but use the new java.time classes.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for you help but how do I return the object
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:Thank you for you help but how do I return the object


https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

Henry
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read the article on returning a class or interface but I still do not know what I need to do I add a new method to the class called RecardObj



I get an error incompatible types at run time.
Can someone show me in code how to do this or a page that will give java object return type example
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You may need to give us more details ... ie. TellTheDetails.

We don't exactly know what you changed, nor the exact error message that you got.

Henry
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added this to the class recardObj
Now in netbeans i get this error on the line with the new code "Incompatible type: int connot be converted to RecordObj …."


 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:I added this to the class recardObj
Now in netbeans i get this error on the line with the new code "Incompatible type: int connot be converted to RecordObj …."



"public RecordObj returnCurrentHour()"

Let's take this method signature and examine it. First it is "public" which means anyone can call it. So far so good. Second, it's return type has been set to "RecordObj." Is that what you wanted to do? Third, it is named "returnCurrentHour()" suggesting you want to return the current hour. What type is the currentHour variable? Does it match the return type of the method? This is what the compiler is trying to tell/ask you.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, the change is not really what was suggested by Stephan... I believe the suggestion was referring to the CurrentHour class.

Henry
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then can you show me how to access the same object in multiple classes.
1) I would like to make a class and turn it into an object called Obj. like a getters and setters

2) Next make a new class lets call this class A and store a value in one of the fields in the Obj getters and setters.

3) Then make a new class called class B and access the values from the Obj class and print them out.

That is all I am looking for is how to pass values into a object from one class and access that same object from other classes.
Can someone show example code or tell me what I need to change in my code to make this work.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to pass the object around.

You say that you want to create an object, but you can't do this without context. Where is the object created? If you want to do all of this in the same method, you first create the object using the new keyword, then you pass it to the appropriate method of class A, and then you pass it to the appropriate method of class B.

Your original code differs from this in that you first call the method of A which creates its own object. You should first create r, and then pass it to currentHour().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!