aspose file tools*
The moose likes Java in General and the fly likes Calling classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling classes" Watch "Calling classes" New topic
Author

Calling classes

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I had created a String array with names in it and I get input from console and compare it with the String array. If both are equal then I am supposed to call the classes that have the same name as that of the values in the String array. Take a look

String classNames[] = { "One", "Two", "Three", "Four" };

and I get input from console

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String inputValue = bufferedReader.readLine();

if(inputValue.equals(classNames[i])) then

call the respective class like

One on = new One();
on.display();

I was advised not to follow

if(inputValue.equals("One")) {
One on = new One();
on.display();
} else if(inputValue.equals("Two")) {
Two two = new Two();
two.display();
}

this kind of coding.

Please help me to solve this problem


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Advised by whom? Is this a class assignment? If so, we won't give you the answer, but we will help you along the way.


James Carman, President<br />Carman Consulting, Inc.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Hello Mr. James Carmen

I am not a Student, and this is not a class assignment. I am a junior developer and my senior ordered me to change the way I presented my code. So I was looking for a alternative. Please don't jump to conclusion before knowing about others. God bless you.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I don't think I really jumped to any conclusions, but we do have a lot of people asking us to do their homework for them. Anyway, since you accused me of jumping to conclusions, I won't just give you the answer. I'm gonna make you work a bit. Here are a few questions to guide you on your way...

  • How do you know that the classes identified by the Strings in the array are going to have the display() method?
  • What would happen if I introduced a new class to the system called Five? Would your code have to change?
  • Do you know of another way to refer to a class by name in Java?


  • [ May 06, 2005: Message edited by: James Carman ]
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026

    How do you know that the classes identified by the Strings in the array
    are going to have the display() method?


    There is lot of XML involved in this project. The display method is
    going to call a xml file do some processing and going to print the element
    values.


    What would happen if I introduced a new class to the system called
    Five? Would your code have to change?


    The number of classes is fixed. It will be 10 for time being. Dynamic
    growth will be added later.


    Do you know of another way to refer to a class by name in Java?


    I don't know this. That's what I am looking for. If you can get me the
    answer definetely you are going straight to heaven.
    James Carman
    Ranch Hand

    Joined: Feb 20, 2001
    Posts: 580
    Well, how could I pass up a guaranteed pass into heaven!?!?!

    What I meant by my question about the display() method, was how do you know for sure that the object is going to have a display() method? Do the One, Two, Three, and Four classes implement an interface or extend a common superclass? If not, you'll have to do a bit more work.

    You need to look into reflection to clean up your code. If you use reflection, and classes are added to the system, your code will not have to change. You will be dynamically invoking code at runtime based on the name of the class. Here's a bit of code to get you started...



    This code assumes there is a superclass or interface (Displayable) which you can cast to that contains the display() method. If not, you're going to have to dynamically lookup the display() method using reflection.
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026
    Thank you very much Mr. James


    Well, how could I pass up a guaranteed pass into heaven!?!?!


    Because you are Mr.President
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026
    I still have problems.



    Inside the try catch block instead of "Displayable" I should specify that particular class to cast. But I got all the class names in a string array. How to resolve this.
    Shyam Prasad Murarka
    Ranch Hand

    Joined: May 02, 2005
    Posts: 209
    Dear vishnu,
    I'll try my best to explain what I can because I am not an expert.

    (( Displayable )Class.forName( classNames[i] ).newInstance()).display();


    In this statement the object that is being instantiated (suppose "One") it is then automatically "upcasted" to the Displayer class. During run-time, java uses "late-binding" technique to distinguish its actual type. Therefore, the display() method of the "One" class is called.

    If you make classe for "Three", "Four", "Five" also and run the program you will get the required result.


    With Best Regards,
    Shyam Prasad Murarka
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Calling classes
     
    Similar Threads
    problem in loop
    2d array
    How to take input from screen automatically
    collecting input using arrays
    java console input doesn't stop linux "rpm" execution