• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot refer to a non-final variable inside an inner class defined in a different method

 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Created a java.sql.connection object.

Refering those obj inside
public void run() {


}

If i declare as final inside a method, i can't refer those outside method due to scope.

Cannot refer to a non-final variable dbConnObj inside an inner class defined in a different method
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would post some sample code as the question isn't very clear.

You can't refer to a non final variable declared in a method from an inner class as I think you are mentioning.
If I did require a non variable shared in this way the obvious solution would be to use a member of the parent class, so declare a member variable in the parent class say public Object wibble; in class OuterClass say and then you can access it from your (non static) inner class with OuterClass.this.wibble and this can be non final.

The issue is the method and its stack can die go before the inner class, the JVM would have to create a copy on the heap.

 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..
This is the code snoppet.
How to initialize and create dbConn (Connection) object?
Have to access inside run().

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code refers to dwThread ie dwThread.processDWRecords(dbConn); and creates a pwThread it never starts, are they meant to be the same thing ? (The main will immediately kill the db conection without waiting for syncthread (use join somewhere) and the syncthread start is in the wrong place, it should have been after the } ).

I would concentrate on getting the threading correct first ie just use System.out.println to simulate how you would like the code to work and the we can fix the db connection resource bit later as the threading as it stands doesn't make sense.

I think first you need to define how long you wish the dbConn to live for eg the liftetime of the SyncThread , the ProcessThread or the main, if its the synch thread you can just create it in the run.

Currently the code doesn't really make sense as you might as well have just created a db connection in the main method processed the records and then closed the connection (no multithreading), I suspect your looking for a way of processing records in another thread so your main thread is free to do something else as yet undefined, in which case you don't need the process thread and you can just create and destroy the connection in the run,. You may have wished to share the dbconnection with other threads in which case its more complicated.

 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is pwThread.processDWRecords(dbConn);
sorry for the typo..

Each thread should run independently and create new connection and close the connection once it update the DB.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, The example I outlined (ie put the connection creation/destroy in the run / local to the method) was a connection per thread so that should work, in a production application you should consider database connection pooling , rather than creating and destroying the connections.
 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..

Now i am coming to the original question.
issue i faced is connection object declaration.

where i need to declare and intialize the connection object (dbConn)?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just create it as per the comments below, it can be non final there, get this working and then if you want something more complicated eg inter thread communication we can add that ..

 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

always getting same connection if intialized inside main()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!