• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding newInstance()  RSS feed

 
Sagarika Seetaraman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to create an instance where the class is known only at runtime.

package org.tt;
// class1
public class CheckedClass
{
public CheckedClass()
{
System.out.println("Inside constructor");
}

public void display()
{
System.out.println("Inside the display method of CheckedClass");
}
}

// class2
import org.tt.CheckedClass;
public class Check
{
public static void main(String arg[])
{
String str = "org.tt.CheckedClass";
Object obj = (CheckedClass)(Class.forName("org.tt.CheckedClass").newInstance());
obj.display();
}
}



When i try to compile the Check class... it gives me an error... "Cannot resolve display"... Why does this happen?

Thanks in advance
Antony
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66264
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Asterix Antony",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66264
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've declared obj as an Object which has no display method. You are casting the object to a CheckedClass, but assigning it to an Object.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66264
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. This probably doesn't really belong in the advanced forum.
 
Sagarika Seetaraman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
P.S. This probably doesn't really belong in the advanced forum.



Thanks a lot.....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!