• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Possible to initialize a field in the arguments of a method?

 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was wondering if it is possible to initialize a field as argument?
What i want to do:
Armor:


a method that just returns the long ID

See the reason i cant do it in the constructor itself is because it inherits from a class called Items, and in a method in items i need the ID, Since a super() has to be first line i have a problem.

What i thought about doing:

making wantedID a field in armor and:

this doesnt work though but if somehow i can do it before the constructor of item is executed my problem would be solved


Item:


So it comes down to, i need a method generateID() in my class armor that just returns the wantedID

Any help would be greatly appreciated! Thanks in advance

Kenny
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using inheritance the superclass's constructor is always the first thing called in the sub-class's constructor. You could always write a getID() method that returns an id for a specific instance of the armor class.

Or instead of writing a getID() method or using generateID() in the Item class, you could make the Item class constructor take in an ID.




-Hunter
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but this isn't allowed though (it's a project we got in school), see the actual ID is final and has to be generated "behind the screens" in other words without the user knowing anything about it, for all the classes inheriting from item, except for Armor. So item can't have an argument. Thanks anyway! any idea's are always greatly appreciated!

Kenny
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the ID is generated "behind the scenes" then why do you allow an ID to be passed in?
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its behind the scenes for everything EXCEPT the class armor, so they all inherit from item and they are all generated except for the one from armor that one is given by the user. Thanks in advance for any advice!
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so item can't have an argument
why
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, as said, Item gets an automatically generated ID. It can't be specified by the programmer.

The question here is why Armor needs a specified ID if it already gets a generated ID.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't we have an overloaded constructor to cater to the needs of Armor ?
In that case ID will be set by Armor else it gets its value from a process which generates it.
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Yogesh

Thats a pretty good idea though. the problem is that if that constructor would be used to make a weapon or another item then i cant do give it an id.. and this still leaves me with the problem i gotta be able to check that it is a prime number and if not make it one. so i wanted in generateID() to be able to get the ID that was used with the creation of a new armor and check if it is prime and if not make it so, this is not that important though. the key here is that i need to be able to have access to a variable that contains the ID before the super() is executed. Thanks a lot for the help!!

Kenny
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob, it doesnt get a generated id, the thing is i would like to use that method to return the id if its a valid one(a prime). Since in item i do ID = generateID() and ID is final(and must be) Thanks in advance!!

Kenny
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Armor inherits the ID from Item. Since Item always gets an automatically generated ID, so does Armor.
 
Kenny Kuchera
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ rob: Exactly... but generateID() is an abstract method that is overrided in weapons,... So for armor it should return the ID so i can assign it to the field ID...
Thanks!

Kenny
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm completely lost. Maybe a class diagram, including the fields and methods in question, and a re-statement of the problem would be a good idea?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic