Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Accessing a method of one class in another class

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Accessing a method of one class in another class

    Hi,
    I am trying to access the method of one class in another class through an object of the former class.
    Constraint is that i have the class name of the first class in a string variable.
    i tried the following code
    but i am unable to access the method..
    package experiment;
     
    public class t1 {
    	public void preent(){
    		System.out.print("test works");
    	}
     
    }
    --------------------------------------------------
    package experiment;
    import java.lang.reflect.*;
     
     
     
     
    public class maiin {
    	static Object createObject(String className) {
    	      Object object = null;
    	      try {
    	          Class classDefinition = Class.forName(className);
    	          object = classDefinition.newInstance();
    	      } catch (InstantiationException e) {
    	          System.out.println(e);
    	      } catch (IllegalAccessException e) {
    	          System.out.println(e);
    	      } catch (ClassNotFoundException e) {
    	          System.out.println(e);
    	      }
    	      return object;
    	   }
     
     
     
    	public static void main(String[]args) throws ClassNotFoundException{
    		maiin M = new maiin();
    		Object O = M.createObject("t1");
    		O.preent();
     
     
    }}

    help me

    Thanks in advance
    Last edited by helloworld922; March 22nd, 2010 at 06:26 PM.

  2. The Following User Says Thank You to Sai For This Useful Post:

    ea09530 (April 4th, 2010)


  3. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Accessing a method of one class in another class

    preent isn't a method of type Object, you must type cast because the object has been polymorphed.

    		maiin M = new maiin();
    		Object O = M.createObject("t1");
    		if (O instanceof t1)
    		{
    			// just making sure it's a safe cast
    			((t1) O).preent();
    		}

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    Sai (March 23rd, 2010)

  5. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Accessing a method of one class in another class

    thanks helloworld922...that does work

    but i am encountered with this exception always...which is not allowing me from creating the object

    Severity and Description Path Resource Location Creation Time Id
    Class is a raw type. References to generic type Class<T> should be parameterized experiment/src/experiment maiin.java line 11 1269321729460 221

    can u suggest me any thing regarding this
    Last edited by Sai; March 23rd, 2010 at 12:31 AM.

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Accessing a method of one class in another class

    The class Class has a generic type. You can use a wild-card because you don't know what type it's suppose to be.

    Class<?> classDefinition = Class.forName(className);

  7. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Accessing a method of one class in another class

    I have tried

    Class<?> classDefinition = Class.forName(className);

    but still its throwing an exception

  8. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Accessing a method of one class in another class

    but there is a problem..when i can use "t1" directly why will i store it in a string variable?...
    the problem is i can just use the string(i.e classname) so , typecasting with t1 ( as in (t1) O) doesnt make sense in my context..
    i can just use
    t1 T = new t1();
    T.preent();
    instead which simplifies the job ...
    i need something that just use the classname not the class
    Last edited by Sai; March 23rd, 2010 at 01:24 AM.

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Accessing a method of one class in another class

    ... It works for me.

    Here's the full code I'm using:

    public class Test
    {
    	public static void main(String[] args)
    	{
    		String classname = "t1";
    		try
    		{
    			Class<?> test = Class.forName(classname);
    			Object o = test.newInstance();
    			if (o instanceof t1)
    			{
    				((t1) o).print();
    			}
     
    		}
    		catch (ClassNotFoundException exception)
    		{
    			// TODO Auto-generated catch block
    			exception.printStackTrace();
    		}
    		catch (InstantiationException exception)
    		{
    			// TODO Auto-generated catch block
    			exception.printStackTrace();
    		}
    		catch (IllegalAccessException exception)
    		{
    			// TODO Auto-generated catch block
    			exception.printStackTrace();
    		}
    	}
    }

    and the t1 class:

    public class t1
    {
    	public void print()
    	{
    		System.out.println("t1 works");
    	}
    }

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  3. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM
  4. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 08:29 PM
  5. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM