• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tracking a constructor to see if it recreate an object  RSS feed

 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there. it's been some time now that i started to learn programming and i started with java. so there is a book that i'm on it right now called "Pearson Absolute Java 5th Edition" by Walter Savitch
anyway i'm on a project in fifth season which i have to create a class named HotDogStand that operates several hotdog stands distributed throughout town. the whole program is clear.
although its so easy to accomplish , my question is more about debugging. so here is the code:


everything works fine. but my question is what if someone use a constructor again? you see if in the main method someone do this after creating the object "stand3":


i know that it's logical to use setter method but this program doesn't have one. if someone do the thing i wrote above, the calculation of static variable, totalHotDogs will be all wrong. because of totalHotDogs += hotDogsPerDay; it will add another value for the same object.
so if i want to fix this intentional bug how can i tell the machine to ignore the second (or more) invocation of the constructor for the same object?

thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohammad tonino wrote:
i know that it's logical to use setter method but this program doesn't have one. if someone do the thing i wrote above, the calculation of static variable, totalHotDogs will be all wrong. because of totalHotDogs += hotDogsPerDay; it will add another value for the same object.
so if i want to fix this intentional bug how can i tell the machine to ignore the second (or more) invocation of the constructor for the same object?


Well, it's not wrong. There are four HotDogStand objects in the heap. It is just that only three of them are reachable. I guess a possible option is to add a finalizer() that will decrement the count when the object is GC'ed.


And BTW, your class isn't thread safe -- but you probably already know that.

Henry
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you wouldn't even need to use a setter method. Even though your instance variables are marked private you can still modify their values with a dot operator, provided you are in the class. Instead of instantiating a new object and assigning to an already existing reference, just change the instance variable values directly.



Or and also, the constructor you provide will always increment the static variable by the value passed through the constructor. You need to change the constructor so it decrements any value from an object that has been reassigned. There are lots of ways you could do this, taking the value away when the object goes to GC (Garbage Collection) or maybe implementing an ArrayList that questions if an object with said value has or is going to be de-referenced. Try some things out and best of luck!
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Henry,
i don't really want to sound stupid but since i'm a newbie, i haven't learned about finalizer() yet, can you explain it a bit more and preferably followed by an example? or is there any alternate or simpler way to do it?

and i don't know about thread safety either. i googled about it and i learned enough to say it is the ability of a class to run multiple tasks simultaneously. but i couldn't find any link about it to my program. first i thought it is something about privacy leak and using unsafe method and constructors.
could you guide me in the right direction please?
thanks
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Or and also, the constructor you provide will always increment the static variable by the value passed through the constructor. You need to change the constructor so it decrements any value from an object that has been reassigned. There are lots of ways you could do this, taking the value away when the object goes to GC (Garbage Collection) or maybe implementing an ArrayList that questions if an object with said value has or is going to be de-referenced. Try some things out and best of luck!

thanks Skye . i think that's something i need to learn about. now that i run a search through my book about GC and ArryList i figured its all there, but i think i'm just obsessed with going ahead of book because i'm so thirsty to learn more material which is absolutely needed for coding. i really appreciate your help.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no guarantee that a finalizer will help. It will not be called until the object is GC‑ed, so it will probably never be called in a small application like this.
We get threads about how many instances of the class are there often enough. People usually conclude that it is easy to create a counter which counts instances as they are created but very difficult to count when they go out of scope and out of reach. I like SA's suggestion about putting all instances of this class into a List; you can use the size method to count how many remain.
You do have to use remove() whenever you want to get rid of an instance.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest way to implement a counter isYou can see a more sophisticated method in Tushar Goel's Account class.

Actually, I think the title of the thread is a bit confusing. It says
Tracking a constructor to see if it recreate an object
… but we know that every constructor call is associated with a new object and a constructor is only called when a new object is created, so it can never re‑create an object.
If you want to have any number of objects, all distinct, rather than storing them in a List, store them in a Set. You must override equals and hashCode correctly or for a tree set implement Comparable or provide a Comparator.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote: . . . You need to change the constructor so it decrements any value from an object that has been reassigned. . . .
How on earth are you going to do that inside the constructor? You cannot tell inside the constructor whether an object is reassigned or not. Remember the constructor is called only once in the lifetime of an object. And overloaded constructors might never be called.

Your List suggestion, on the other hand, is good.
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The simplest way to implement a counter isYou can see a more sophisticated method in Tushar Goel's Account class.


thanks Campbell. yes, i think that's what i can use right now with the material i'v learned so far. Easy to understand
and by the way, by "recreating an object using constructor" i meant using a constructor as a mutator like i said in my first post. sorry i didn't know what the scientific term is thank you
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A mutator is a method which can be called many times. A constructor cannot be called many times. I don't understand what you mean by using a constructor as a mutator.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How on earth are you going to do that inside the constructor? You cannot tell inside the constructor whether an object is reassigned or not. Remember the constructor is called only once in the lifetime of an object. And overloaded constructors might never be called.

Your List suggestion, on the other hand, is good.


I see this now and realize my mistake. Thank you for pointing that out.


thanks Skye . i think that's something i need to learn about. now that i run a search through my book about GC and ArryList i figured its all there, but i think i'm just obsessed with going ahead of book because i'm so thirsty to learn more material which is absolutely needed for coding. i really appreciate your help.


Of course. Learning Java, or any new skill, can go in many ways. Since you have a 'thirst' for Java I recommend you use that to your advantage. If you find something that you want to implement into code it may be in your best interest to find a section in your book that explains what said implementation will do. For example, I recommended an ArrayList to maintain your hotDogStand instances. Reading about this will further your knowledge of Java and give you instant gratification as you see it go to work in your code. There is always the boring method of reading Chapter 1 and then moving onto Chapter 2 and so on until you're done with the book, but that can get pretty boring and tedious. However, not to say that way is ineffective. Read the book by having patience and let your thirst fuel you as you progress through the language. It is tempting to feel you need to be an expert programmer with every piece of the API at your fingertips in the beginning. Have patience, my friend, you will get there ^_^.

And again, best of luck!
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:A mutator is a method which can be called many times. A constructor cannot be called many times. I don't understand what you mean by using a constructor as a mutator.




this is what i mean. the second line is also possible right? sorry i don't know what to call it. but from a greenhorn point of standing it is using a constructor as a mutator. isn't it a mutator's work to change the value of an instance variable? anyway i just wanted to avoid such an unwanted change in an instance variable which i accomplished thanks to all of you great guys.

Skye Antinozzi wrote:

thanks buddy. very encouraging. \/
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ngml Bishop wrote:


this is what i mean. the second line is also possible right? sorry i don't know what to call it. but from a greenhorn point of standing it is using a constructor as a mutator. isn't it a mutator's work to change the value of an instance variable? anyway i just wanted to avoid such an unwanted change in an instance variable which i accomplished thanks to all of you great guys.


Perhaps it is a terminology issue then. In general, the term "mutate" refers to changing the object, and not really to a reference. In your example, you created a new object, and changed the reference to point to it.

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is called an assignment. You are not changing the state of the first object at all. You may be discarding it, but we cannot tell by seeing only part of the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!