File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Generic error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic error" Watch "Generic error" New topic
Author

Generic error

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


Box.java:13: error: non-static type variable String cannot be referenced from a static context
public static void main(String[] args)
^
1 error


My one regret in life is that I am not someone else.
- Woody Allen
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

In this example "String" does not mean what you think it does.

It is not the name of the class java.lang.String. Instead, you've declared a type parameter in line 1, which is confusingly named "String".

Normally people use single upper case letters for type variables, for example "T", so you'd write "class Box<T>" in line 1. But you can name a type parameter anything you like, including the name of an existing class such as "String".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thank you, why that compile error occurred ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

As Jesper said, you've used String as a generic type variable, and it's clashing with the class String. The generic type takes precedence, so on line 10 that's what the compiler things you're referring to. And because the type variable applies to specific instances of Box, it can't be used in a static context.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic error
 
Similar Threads
how to replace the bracket in java?
Hi to all
Check String for null without error?
IF strings are immutable why is the variable s appending and changing its obect
Very Basic Question ....