• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to keep an object in scope

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.












 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for you help but how do I return the object
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 …."


 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic