GeeCON Prague 2014*
The moose likes Java in General and the fly likes Illegal static declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Illegal static declaration" Watch "Illegal static declaration" New topic
Author

Illegal static declaration

Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
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

Joined: Feb 05, 2004
Posts: 105

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.


Website: Learn Java by Examples
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
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

Joined: May 26, 2003
Posts: 30586
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Adam Burda
Ranch Hand

Joined: Jul 19, 2011
Posts: 40
Ok sorry about the code. I was only showing the inner class here is the whole code.

Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Jul 19, 2011
Posts: 40
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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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?
 
GeeCON Prague 2014
 
subject: Illegal static declaration