This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes cannot make static reference to non-static Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "cannot make static reference to non-static Integer " Watch "cannot make static reference to non-static Integer " New topic
Author

cannot make static reference to non-static Integer

Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
I get the same Java Error in at line 21: cannot make static reference to non-static Integer

for this line:
Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer


or I tried using the primitive type int:
int intResult = result; //error: cannot make static reference to non-static Integer

but really I need to get it to be an object Integer

but i still get the same error.

What I am trying to do is read integers until a user enters Ctrl-C. But I am afraid writing this in Java doesn't seem be the same way as C.


(1) I would greatly appreciate some help in getting rid of error and (2) also how to make it stop requesting user input at Ctrl-C without getting exception error. Some example links would also be helpful.



I'm not very experience with Java. I know how to do it in a C program but Java seems much complicated to me anyways.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Ana Suvari wrote:
Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer


This is not a valid Java statement... When you use the new operator, it is expecting the class name and constructor parameters. And when you try to call a static method, it is not expecting the new operator to be used. There is no such a thing a trying to instantiate a call to a static method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Henry Wong wrote:
Ana Suvari wrote:
Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer


This is not a valid Java statement... When you use the new operator, it is expecting the class name and constructor parameters. And when you try to call a static method, it is not expecting the new operator to be used. There is no such a thing a trying to instantiate a call to a static method.

Henry


I originally had it without the "new" operator but I had the same error. I decided to put "new" because Integer is a class. With or without "new", i get the same error. How is result a static? It's not supposed to be. intResult isn't a static. How did one of those become static?

Thanks for your patience.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I originally had it without the "new" operator but I had the same error

Really ? Can you try again, and copy/paste the line which gives you that same error ?


[My Blog]
All roads lead to JavaRanch
Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Christophe Verré wrote:
I originally had it without the "new" operator but I had the same error

Really ? Can you try again, and copy/paste the line which gives you that same error ?


Okay, I'll display my both files instead of just the piece. The error is on line 152 in T.java

T.java:



TreeNode.java
Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Please someone help me. Once I get this going, then I could really start on my assignment. It's incomplete because I can't get the basics tested.

I would try to finish all my functions but what is the point if i can't test it out.

I wish that there was an easier way to learn Java.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Why are you declaring T as <Integer extends Comparable> ? This hides the Integer class, and you're not using it anywhere. Remove it if you don't need it.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

And you should give your packages and classes better names. "T" is not an understandable class name, and packages should be lowercase.
Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Christophe Verré wrote:Why are you declaring T as <Integer extends Comparable> ? This hides the Integer class, and you're not using it anywhere. Remove it if you don't need it.


I was using it but then I got too many errors and realized I didn't know enough about how it's supposed to work to fix those errors so I removed most of it's usage.

But i'm still not sure how that is related to the error that I'm talking about.

The crazy thing is at one time it was able to compile. I'm not sure what happened but I didn't keep a back up.

I don't understand Java. I wish I could learn Java but it seems like I have a long way to go. C was easier.

Oh well. I tried. I failed but I tried.

Thanks anyways.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

To declare that a class can use a generic type, you will declare it like "public class MyClass<T>", where T is a generic type. You can use T as a generic type in the class. T is a name you give to the generic type. It can be named anything. "public class MyClass<GenType>" would also be correct. What happens if you use "public class MyClass<Integer>" ? It means that Integer is used to refer to a generic type. The bad news is that it also hides the real java.lang.Integer class. Anything referring to Integer in your class will not refer to java.lang.Integer, but to your generic type.

I don't understand Java. I wish I could learn Java but it seems like I have a long way to go

Maybe you're trying to go too fast. Any new language needs some time to adapt to. Don't give up.

C was easier.

That's debatable A language we already know always looks easier than a language we don't know.
Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Hey that's strange. I removed that <Integer extends Comparable> and now the error is gone .

I don't see how that could have caused the error. I'm glad the error is gone but I'm not happy that it wasn't something that I can explain and understand Java.

There's too much that I don't know. Still i long way to go.

Hoping for no more crazy errors that i can't explain.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


If you rename the generic type, this will compile :

Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
Thanks so much for your explaination
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cannot make static reference to non-static Integer