home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2532
Discussions: 186,605, Posts: 2,227,032, Members: 230,239
Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
Old September 26th, 2008, 09:22 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Sep 2008
Posts: 5
variable classes but same method

In JAVA I have 26 classes named A, B,.......,Y,Z. Each of them implements a method named Meth().

I have a String str that can contain the letter "A" or "B" or ....... or "Y" or "Z".

How can I implement the statement str.Meth();

so it will go to a different method depending on the value in str.

Thank you

zscipio is offline   Reply With Quote
TechIMO.com Ads - Login or register for less ads.
How many errors does your computer have?

You no longer need to guess! This free stability scan and registry cleaner download will give you a complete diagnosis of your Windows registry, identifying errors and conflicts.

FREE instant scan


Guest, Register Free! to remove this ad and get your tech support questions answered in minutes!
Old September 26th, 2008, 12:32 PM     #2 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,052
Send a message via ICQ to Creatures
so the methods do different things? or are the really the same just in different classes which seems to be kind of strange

could you state your problem, maybe there is a simpler or other way of solving it

Creatures
__________________
Knowledge Is Power

Creatures is offline   Reply With Quote
Old September 26th, 2008, 03:25 PM     #3 (permalink)
Junior Member
 
Join Date: Sep 2008
Posts: 5
The methods do different things depending on the class. I could use an IF ELSE construct and just list them all or use a SWITCH CASE construct but I am trying to do it in one statement.

zscipio is offline   Reply With Quote
Old September 26th, 2008, 04:53 PM     #4 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,540
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
So essentially you have Class1, Class2, Class3 all of which implement Meth()

You have a string that contains the name of a class.
So str = "Class3";

You want to turn something like
Code:
switch(str){
   case "Class1":
            Class1.Meth();
}
Into a one line statement in Java?
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
jkrohn is online now   Reply With Quote
Old September 26th, 2008, 05:02 PM     #5 (permalink)
Junior Member
 
Join Date: Sep 2008
Posts: 5
Yes you have it.

Thanks.
zscipio is offline   Reply With Quote
Old September 26th, 2008, 05:12 PM     #6 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,540
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
I don't do Java, but this appears that it would work.

Transitioning into OOP: Intricacies of Java object construction and initialization

Specifically
Code:
Object   anObject = Class.forName("com.acme.utils.Foobar").newInstance();
anObject.Meth();
jkrohn is online now   Reply With Quote
Old September 27th, 2008, 12:07 AM     #7 (permalink)
Junior Member
 
Join Date: Sep 2008
Posts: 5
Unhappy
Sad but it didnt work.

When I tried
Object anObject = Class.forName("AK").newInstance();
anObject.Transform();
the compiler said "The method Transform() is undefined for the type Object ".
When I tried
((AK) anObject).Transform();
it compiled

When I tried
Class aClass = (Class) Class.forName("AK").newInstance();
aClass.Transform();
the compiler said "The method Transform() is undefined for the type Class".

When I tried
AK.Transform(); it compiled.

For some reason the class name must be mentioned.
zscipio is offline   Reply With Quote
Old September 27th, 2008, 10:32 AM     #8 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,741
Send a message via AIM to Rootstonian
Not 100% sure what you're trying to do??? So if str = A or B, you want to call A.Meth()???

Hmmm...seems to me when you instantiate the class, you'll have access to the correct Meth(); function; I just don't know what you're trying to do LOL

void main() {

A myClassA;
myClassA.Meth();
B myClassB;
myClassB.Meth();

// do you want this?

if(str == 'A')
myClassA.Meth();

}
Rootstonian is offline   Reply With Quote
Old September 27th, 2008, 10:26 PM     #9 (permalink)
Junior Member
 
Join Date: Sep 2008
Posts: 5
Smile
Solution

To all those who gave suggestions a hearty thank you. The final solution was a follows:

An interface was set up.
************************************
package writeTextFile;

public interface WriteFile {
public void Transform();
}
************************************
All the classes implement the interface such as:
************************************
package writeTextFile;

public class AK implements WriteFile
{
public void Transform()
{
System.out.println("Went to AK");
return;
}

}
************************************************** ********
The 2 statements necessary to execute the transfer were:
************************************************** ************
WriteFile t = (WriteFile) Class.forName("writeTextFile." + state).newInstance();
t.Transform();
************************************************** ************
where the string state contained the 2 character class name.
The important but not obvious point was that the package name "writeTextFile" had to be included.

Thank you again for all your help.
zscipio is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual classes in C# ShuckyD Webmastering and Programming 3 April 12th, 2006 02:58 PM
calling upon classes in C# ShuckyD Webmastering and Programming 3 February 1st, 2006 05:32 PM
License Classes JimRune IMO Community 2 August 29th, 2005 12:48 AM
making a variable get used with a variable DKY Webmastering and Programming 2 June 14th, 2003 09:53 AM
Passing a class object variable to another object variable ILC Webmastering and Programming 1 March 1st, 2002 02:10 AM

Most Active Discussions
Is It Just Me? (524)
Misery Loves Company... (1849)
Why Does the MOON Grow Bigger as It.. (17)
heatsink issue (10)
New Mobo (18)
UPGRADING C/D DRIVE TO 250GB & .. (13)
SSD's, RAID, and External Backup (7)
1 internet. 1 house. 3 computer. ho.. (13)
Is This A Compatible Gaming PC? (18)
Recent Discussions
UPGRADING C/D DRIVE TO 250GB &a.. (13)
PNY vs Gainward (3)
Linksys WRT54G (2)
Contest: Logo and Banner design.. (0)
Hotmail Q (2)
SSD's, RAID, and External Backu.. (7)
Laptop doesn't stay connected t.. (7)
Computer won't start (4)
Official World of Warcraft Thre.. (4528)
FS: Dell 6000 laptop, modded 36.. (2)
Apple iPod touch 16 GB $200 (4)
Six 28-Disc Cross Design Black .. (4)


All times are GMT -4. The time now is 11:10 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28