Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes  Instantiate a new object inside a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Instantiate a new object inside a class" Watch " Instantiate a new object inside a class" New topic

Instantiate a new object inside a class

akash gall

Joined: Feb 07, 2013
Posts: 3
Hi everyone,

I have this requirement where I need to instantiate a new object inside a class (from a jar), its like a jdbc connection manager.

From a testMethod() (which is static) I am calling my class (which contains a reference to this object). The class looks like this:

public class myClient {

public static MyClientManager manager = new MyClientManager();

public static String validateConnection() {
return null;

From testMethod, I am going myClient.validateConnection() and its always failing. If I remove this line (public static MyClientManager manager = new MyClientManager()) it works.

Please help.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

What do you have in MyClientManager class?
Show the whole code. and where is "this" reference used?

Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 867
Maybe the constructor in the very line throws an exception preventing the class from being initialized.

I agree. Here's the link:
subject: Instantiate a new object inside a class
Similar Threads
Protected Method
Problem with Junit control flow.
Instantiating Interface or Abstract
newbie's question about inner class existence
does static methods inherit ? what about private ?