|
19 Oct 2005, 18:59
|
#1
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Java stuff
Need a bit of help with a java thing im doing.
1. Can you define an objects name using the constructor?
2. How do you use the methods of an object you dont know the name of?
I have an object of a random name and I write a method the takes a string parameter which is the name of an object, how can I use the method of the object that was supplied as a parameter.
E.g. Usual is account1.deposit(5000).
I need to supply "account1" as a parameter and have it execute that method (obviously without writing "account1" in the source code).
Thanks.
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 19:24
|
#2
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
Quote:
Originally Posted by Eidron
Need a bit of help with a java thing im doing.
1. Can you define an objects name using the constructor?
|
I'm not sure what you mean. You can either declare the name/type first and then use the constructor later, like this
Code:
MyClass what;
; code here
what = new MyClass();
or you can do it all in one go
Code:
MyClass what = new MyClass();
Quote:
2. How do you use the methods of an object you dont know the name of?
I have an object of a random name and I write a method the takes a string parameter which is the name of an object, how can I use the method of the object that was supplied as a parameter.
|
I'm not entirely sure what you mean here, but I'm guessing you want to do something like this.
Code:
public static void myMethod (MyAccount acc) {
acc.deposit(500);
}
public static void main (String[] args) {
MyAccount account = new MyAccount();
myMethod(account);
}
The method youre calling doesnt need to know the actual 'name' of the object that was used to call it. A 'name' of an object is essentially just an address that lets you find whereabouts the object is stored in memory. What happens in the above code isnt that the main() method tells myMethod that the object is called 'account' - it just gives it the address of the object which is being denoted by the name 'account'. The actual name doesnt matter. Hence the names 'account' and 'acc' both names of the same object (if that makes sense, which it probably doesnt).
Last edited by Nodrog; 19 Oct 2005 at 19:35.
|
|
|
19 Oct 2005, 19:39
|
#3
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java stuff
Not really, in the way you describe. As nodrog has said, the method being called doesnt know what youve called the object in the code
One way to do what you want is perhaps to use a HashMap to store the object alongside a key for it. This key can be a string, so used to hold a 'name' for it.
You can then retrieve the object from the hashmap using this key and perform any operations you want on it
|
|
|
19 Oct 2005, 19:42
|
#4
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
Quote:
Originally Posted by Phil^
Not really, in the way you describe. As nodrog has said, the method being called doesnt know what youve called the object in the code
One way to do what you want is perhaps to use a HashMap to store the object alongside a key for it. This key can be a string, so used to hold a 'name' for it.
You can then retrieve the object from the hashmap using this key and perform any operations you want on it
|
I'm fairly sure he isnt literally wanting to pass a "string containing the objects name" to the function, but then I suppose he could be.
|
|
|
19 Oct 2005, 19:49
|
#5
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Java stuff
my interpretation of what he wants is to be able to dynamically name the object at runtime, which isnt as far as im aware, possible
one way around it that i know of is to use a hashmap to store, and retrieve the objects using the key param as its 'name'
|
|
|
19 Oct 2005, 19:52
|
#6
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
Ok ive done number 2. Just need to do number one know. The program will work as is but not how I WANT it to work.
Well, its not a proper program since im just using bluej to run methods. When you create a new object you set it's name. Is it possible to change the name using the constructor, regardless of what you put in the box (or when using the "new" operator)?
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 19:53
|
#7
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
Quote:
Originally Posted by Phil^
my interpretation of what he wants is to be able to dynamically name the object at runtime
|
But theres no conceivable reason why you would ever want to do that. Any solution that needed to do this would be hideously flawed at some fundamental level.
|
|
|
19 Oct 2005, 19:56
|
#8
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
Quote:
Originally Posted by Eidron
When you create a new object you set it's name. Is it possible to change the name using the constructor, regardless of what you put in the box (or when using the "new" operator)?
|
Can you explain why you want to do this? I think youre confused about the role which 'names' play in Java. However, the following code would work:
Code:
MyAccount account = new MyAccount();
MyAccount account2 = account;
account2.deposit();
'account' and 'account2' are now both names for the same object.
|
|
|
19 Oct 2005, 19:58
|
#9
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
Also is it possible to stop an object from being created depending on what parameters are passed to it?
e.g.
Code:
int x =4;
object1 = new SomeObject( x );
If I wanted x to be 3 for the object to be created, how do I stop the object from being created if 4 is passed to it (and not changing the "int x =4" - that is just an example).
Thanks for your help though.
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 20:02
|
#10
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
Quote:
Originally Posted by Nodrog
But theres no conceivable reason why you would ever want to do that. Any solution that needed to do this would be hideously flawed at some fundamental level.
|
Ok, imagine I have 1 object called "object1" and another object called something else.
Each has a field called "x".
I want to take 2 from x in "object1" and add 2 to the x field in the other object. Now, the method that does this wants to have the name of "object2" supplied as a parameter.
As for changing the name, I want to have the objects name as the same as the value one of the fields (a string)...
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 20:16
|
#11
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
Quote:
Originally Posted by Eidron
Now, the method that does this wants to have the name of "object2" supplied as a parameter.
|
No it doesnt.
Your objects are stored in memory somewhere. In order to do stuff to them, you need to know where about in the memory they are - in other words, you need to know their memory address. When you give an object a 'name' like this:
Code:
MyAccount account = new MyAccount();
then what acctually happens is that the name 'account' becomes associated with the memory address of the MyAccount object which you have just created. So, when you later do a command like account.deposit(); what happens is that your computer looks at the name 'account', retreives the memory address which was associated with it, and uses this address to access the account object. But the name itself isnt important, all that matters is the address it is associated with. So for instance, if you have
Code:
MyAccount account1 = new MyAccount();
MyAccount account2 = account1;
then account1 and account2 become names of the same object - the address associated with both names is identical. So the following statements:
account1.withdraw();
account2.withdraw();
will be completely equivalent - account1 and account2 are names for the same object, so doing something to one affects the other. So the following code
Code:
MyAccount account1 = new MyAccount();
MyAccount account2 = account1;
account1.setBalance(100);
System.out.println("account1 balance is" + account1.getBalance());
account2.deposit(100);
System.out.println("account balance is" + account1.getBalance());
will produce this output:
account1 balance is 100
account1 balance is 200
ie, 'account2.deposit()' has increased the amount of money in the object denoted by account1, because the 2 names are both referencing the same object in memory
So...
When youre passing objects to functions, you dont need to actually tell the function what the 'name' of the object is (since the object can have lots of different names)- you just need to tell it what the memory address is. So when you do something like this:
Code:
; transfer 'amount' from acc1 to acc2
public static void transferMoney (MyAccount acc1, MyAccount acc2, float amount) {
acc1.withdraw(amount);
acc2.deposit(amount);
}
public static void main (String[] args) {
MyAccount account1 = new MyAccount(400);
MyAccount account2 = new MyAccount(300);
transferMoney(account1, account2);
}
this will do what you want.
The transferMoney method doesnt care that the objects being passed were previously called 'account1' and 'account2' rather than 'acc1' and 'acc2' - what happens is that the name 'acc1' is associated with the same address as the name 'account1', so that they both refer to the same object in memory (just like above). So doing acc1.withdraw() will withdraw money from the object denoted by 'account1'.
I dont think I'm being particularly clear here, maybe someone else can explain it better.
|
|
|
19 Oct 2005, 20:59
|
#12
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
Ok well I have that part figured out anyway. Works fine.
All I want to do now is to stop an object being created if the result of a certain if() statement is false. I have no idea where to put the if() statement though...
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 21:11
|
#13
|
Emperor
Join Date: Jul 2001
Location: in front of a computer
Posts: 490
|
Re: Java stuff
You should seriously invest more time in understanding the concepts you try to use...
|
|
|
19 Oct 2005, 22:50
|
#14
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
And maybe I should invest more time in explaining my problem better but....meh.
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
19 Oct 2005, 23:41
|
#15
|
The Harsh Light of Day
Join Date: Jun 2001
Location: Behind my comp in Hertfordshire, UK
Posts: 101
|
Re: Java stuff
For any of who see this and want to do the same...look up "IllegalStateException()"
__________________
"I am become death, the destroyer of worlds"
You'll never see me but I am everywhere
|
|
|
20 Oct 2005, 05:47
|
#16
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: Java stuff
Code:
public class IllegalStateException
extends RuntimeException
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.
your calling something before it is ready or is in aviable to use
find what line is causing that error and see what it is doing
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
20 Oct 2005, 16:23
|
#17
|
Warden
Join Date: Jul 2005
Location: The Far Side
Posts: 137
|
Re: Java stuff
Quote:
Originally Posted by Eidron
Also is it possible to stop an object from being created depending on what parameters are passed to it?
|
Wouldn't it be easier to test the variable before calling the constructor? I'm not a Java programmer but it seems pretty obvious, like:
Code:
int x = 4;
if ( x == 3) {
object1 = new SomeObject(x);
} else {
object1 = NULL; /* or whatever */
}
|
|
|
20 Oct 2005, 21:14
|
#18
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Java stuff
You could also have your constructor throw an exception if there was something wrong with a passed parameter.
Code:
public MyAccount (float initial-balance) {
if (initial-balance < 0) throw new NegativeAccountBalanceException("oh no!");
balance = intial-balance;
}
|
|
|
22 Oct 2005, 21:29
|
#19
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Re: Java stuff
Perhaps you should have posted a screenshot of BlueJ. I'm not familiar with BlueJ and I expect it is significantly different from plain Java programming. It might have its own functions for accessing its objects but it would be a hideous perversion to use them. You probably want a hashmap like Phil^ says, and a class for managing the hashmap. eg:
accounts.create(3) // creates account1
accounts.get("account1").balance()
accounts.create(4) // throws exception
In plain Java apps, it is possible to program using class, method and field names using the java.reflect library. It's only ever a good idea, however, if you're doing "meta-programming" - development environments, distributed computing, scripting languages - not "normal" programming. Adding an arbitrarily named field to an existing class is certainly not possible without major hacking.
__________________
#linux
|
|
|
|
All times are GMT +1. The time now is 04:30.
| |