aspose file tools*
The moose likes Beginning Java and the fly likes confused with Object help plz 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 » Beginning Java
Bookmark "confused with Object help plz" Watch "confused with Object help plz" New topic
Author

confused with Object help plz

S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Program 1:

public class hello
{

public void sayHello()
{
System.out.println("Hello");
}

public static void main(String[] args) throws Exception
{
hello instanceOfhello = new hello();
instanceOfhello.sayHello();
}
}

output :
Hello

Program 2:

public class hello
{

public void sayHello()
{
System.out.println("Hello");
}

public static void main(String[] args) throws Exception
{
Class c = Class.forName("hello");
Object obj = c.newInstance();
obj.sayHello();
}
}

Program 1 works fine but program 2 shout at me.....i dont know what went wrong here can anyone help me out plz
Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
Originally posted by S Thanigaivel:
Program 1:

public class hello
{

public void sayHello()
{
System.out.println("Hello");
}

public static void main(String[] args) throws Exception
{
hello instanceOfhello = new hello();
instanceOfhello.sayHello();
}
}

output :
Hello

Program 2:

public class hello
{

public void sayHello()
{
System.out.println("Hello");
}

public static void main(String[] args) throws Exception
{
Class c = Class.forName("hello");
Object obj = c.newInstance();
obj.sayHello();
}
}

Program 1 works fine but program 2 shout at me.....i dont know what went wrong here can anyone help me out plz


By default, every class is a decendant of Object, but in this case, for you to call "sayHello", cast the obj to "hello" class explicitly.
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Hi Thanigaivel,

The program 2 is shouting at because you have not type casted the object properly. c.newInstance() returns an object but this object has to be type casted explictly to "hello" object. So your program should be as below:


Class c = Class.forName("hello");
hello obj = (hello)c.newInstance();
obj.sayHello();

Regards
Sujith
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
It works now

Thank You Mr.Raymond Ong
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Thank you Sujith following codes works well now

Class c = Class.forName("hello");
Object obj1 = c.newInstance();
((hello)obj1).sayHello();

hello obj = (hello)c.newInstance();
obj.sayHello();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with Object help plz
 
Similar Threads
how to solve NoClassDefFoundError while creating XmlBeanFactory object in springs?
Problem while running a client
RMI return BufferedImage object from remote method
Rebind() Problem
Error while registring the server object to RMI registry