• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling constructor..  RSS feed

 
chandraiah chintakayalu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,
Question is:
I have a class A in constructor of class A i have instantiated Connection class as below..

package AA;
import *;
public class A()
{
public A()
{
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");

}
}

and i have another package BB and class B is defined in it,i imported package AA into package B and i want use Connection object which i have created in Class A constructor.

Can anybody tell me how to use that connection object in class B


thanks in advance.
[ January 10, 2008: Message edited by: chandraiah chintakayalu ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment, the Connection object is a local variable inside A's constructor. When the end of the constructor is reached, the variable con goes out of scope, and you can't do anything with the connection object anymore.

You'll have to make con a member variable of class A, and then provide a getter method in class A to return the connection. In class B, you can call that getter method on an instance of class A to get the connection.

A remark about this line:

import *;

Do you really have that line in your source code? That will not compile (at least not if you're using a newer version of Java).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!