• Post Reply Bookmark Topic Watch Topic
  • New Topic

static field in constructor  RSS feed

 
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pets.java:5: error: illegal reference to static field from initializer
name = prefix + s;
^
1 error

I don't understand this problem , why i can't access static field in constructor, what's wrong in it ?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted is not the code that produces that error, because there's no System.out.println in your code.

Please post your actual code.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The code you posted is not the code that produces that error, because there's no System.out.println in your code.

Please post your actual code.


Done
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:I don't understand this problem , why i can't access static field in constructor, what's wrong in it ?

Because the language specification prohibits it: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2-300
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!