*
The moose likes Java in General and the fly likes What's wrong with this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What Watch "What New topic
Author

What's wrong with this code

Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96


What's wrong with this code .....

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method newInstance() from the type Class

at SystemProperties.main(SystemProperties.java:17)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You are running this code from an IDE, aren't you? Check out which errors it is giving you. My guess is that cl is uninitialized.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Simply put: the code above wouldn't give the error shown, as you're not calling newInstance() statically. That error could come about if you literally called something like

Class.newInstance();

but you've changed the code since then. Still, this code will give a NullPointerException, as "cl" isn't initialized.

What are you trying to do, exactly?


[Jess in Action][AskingGoodQuestions]
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
i'm trying to create SystemProperties Object using newInstance() method....i'm not using IDE !! not yet
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Arun Giridharan wrote:i'm trying to create SystemProperties Object using newInstance() method....i'm not using IDE !! not yet


Well, yes -- yes you are. The "unresolved compilation problem" message comes from Eclipse.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Class c1;


cl is only a reference variable.......and if you donot creae its instance then it will give a runtime error.which is nullpointerexceptin


SCJP6.0,My blog Ranchers from Delhi
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

It won't even get that far. Because it's a local variable it needs to be initialized before it can be used. Initializing it to null would cause the NPE.
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
This doesn't work ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Too complicated a question for "beginning Java". Moving thread.

Try
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 516
    
    4

"Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader."

What you're trying to do, is create a class out of nothing and cast it to your own class. I think you have to define the class first. That would be my guess.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Hari haran Ravi
Greenhorn

Joined: Aug 11, 2010
Posts: 26
Arun, what you are trying to do, is that you are calling a non static method named newInstance in the class named "Class". Well, actually, in the class "Class" which is predefined, there are no static methods named "newInstance()".

To make your code work, just try using "SystemProperties.getClass().newInstance()"

I hope it works.

Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I earlier wrote:Try
Actually it's not Class but Class<T>, so it should be Class<SystemProperties>.

And did you notice my mistake writing "class" instead of "Class"? I have only just noticed it. Sorry.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What's wrong with this code
 
Similar Threads
Why recompile?
JDK version question!
windows user name
newInstance() method
How to call program arguments of different class through java code