aspose file tools*
The moose likes Beginning Java and the fly likes Possible to initialize a field in the arguments of a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Possible to initialize a field in the arguments of a method?" Watch "Possible to initialize a field in the arguments of a method?" New topic
Author

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

Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
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

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
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

Joined: Sep 29, 2008
Posts: 12617

If the ID is generated "behind the scenes" then why do you allow an ID to be passed in?
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
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

Joined: Dec 17, 2009
Posts: 133
so item can't have an argument
why
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
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

Joined: Mar 16, 2010
Posts: 9
@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

Joined: Mar 16, 2010
Posts: 9
@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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Armor inherits the ID from Item. Since Item always gets an automatically generated ID, so does Armor.
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
@ 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

Joined: Sep 29, 2008
Posts: 12617

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible to initialize a field in the arguments of a method?