• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal static declaration

 
Adam Burda
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all hope you can help.
I am getting three errors for a inner class. Here is the class.


.\ContactP.java:162: error: Illegal static declaration in inner class ContactP.ContactSql
static Connection conn;

modifier 'static' is only allowed in constant variable declarations
.\ContactP.java:164: error: Illegal static declaration in inner class ContactP.ContactSql
static Statement st;

modifier 'static' is only allowed in constant variable declarations
.\ContactP.java:166: error: Illegal static declaration in inner class ContactP.ContactSql
static {

modifier 'static' is only allowed in constant variable declarations
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't see the lines of code that give you the errors in the portion of the code you've posted. But the error is regarding static in inner class. Unless for declaring a static constant / final variable you can't have static declaration in inner class, for example define a static method in inner class is not allowed.
 
Adam Burda
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the code the static errors are there
line 42
line 44
line 46

So how do i go about fixing it. I tryed to make it as its own class file, and instantiate the class in a nother class.
But i keep getting throw or catch errors. When the class already has the throw and catch exception.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
377
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted doesn't match the compiler errors. In particular, there is no ContactP. The code you posted does compile if I remove the Fn3, etc (since those variables aren't present.)

As a general practice, having static state for Connections isn't a good idea because the connection never gets closed.
 
Adam Burda
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sorry about the code. I was only showing the inner class here is the whole code.

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with JDBC, so I've moved the thread to a better forum.

Inner classes cannot contain static members. This is how the language was designed. This topic was discussed before, see for example this older thread: http://www.coderanch.com/t/417516/java/java/static-declaration-class
 
Adam Burda
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at code line 170, you will see where it has to do with JDBC. It will not load, keep getting errors.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, the cause of the problem is not connected to JDBC, but to the Java language design in general.

You need to change the code so that you don't have static members or initializers in inner classes. As Jeanne has said, it is not a good idea to declare a Connection as static anyway.

Have you read the other thread where the problem was discussed? Do you have any more questions on that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic