• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in global declaration  RSS feed

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please help me out,hope you could spare your patience to read this.

The code goes like this,

<blockquote>code:
<pre name="code" class="core">

package pkgA;

class classA
{
ErrorProperties objErrorProp = new ErrorProp();
Sysproperties objSysProp = new Sysproperties();
.,
.,
..

//I will be creating almost 10 different objects like this
//then

new classB(objErrorProp,objSysProp );
new classC(objErrorProp,objSysProp );

//I dont want to call each and every class like this,
//I need a better way of implementing
//so that only once i intialize and the object initialized exists in only one memory location.
//I will not pass anything i need to make this initializations global and use it when and where requied all over the subpackages of pkgA.


}

package pkgA.subpkgA; // A Pkg inside pkg A

class classB
{

public ErrorProp objErrorProp = null;
public SysProp objSysProp = null;


classB(ErrorProperties ObjErrorProp,SystemProperties ObjSysProp)
{
this.objErrorProp = ObjErrorProp;
this.objSysProp = ObjSysProp;


}

void method1()
{
objErrorProp.readProp();

//accordingly i will use the necessary objects after initialising them in the constructor.


class classC
{

public ErrorProp objErrorProp = null;
public SysProp objSysProp = null;


classC(ErrorProperties ObjErrorProp,SystemProperties ObjSysProp)
{
this.objErrorProp = ObjErrorProp;
this.objSysProp = ObjSysProp;


}

void method1()
{

objErrorProp.readProp();

}


</pre>
</blockquote>

My Question ?

//I dont want to call each and every class like this,
//I need a better way of implementing
//so that only once i intialize and the object initialized exists in only one memory location.
//I will not pass anything i need to make this initializations global and use it when and where requied all over the subpackages of pkgA.

Could some one help me in this/
Also, let me know if there exists a design pattern for this problem

thanks in advance!!



:roll:
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you need to call the constructor with arguments to instantiate the class.
So there is no other better way to do it.
But what is the problem doing it that way.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Ghorpade:
Now you need to call the constructor with arguments to instantiate the class.
So there is no other better way to do it.
But what is the problem doing it that way.



If i have 10 classes, each will have a local variable and needs assignment.
I need to change it to one possibele location so that i will be able to access it any where without passing them as arguments.

I dont want to pass an argument to the constructor.

I need some way to make them Global.

Some one Please help me out !

Am really struggling!
[ July 15, 2008: Message edited by: ram kumar ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with static variables?

<blockquote>code:
<pre name="code" class="core">
class X {
public static final ErrorProperties objErrorProp = new ErrorProp();
public static final Sysproperties objSysProp = new Sysproperties();
}
</pre>
</blockquote>

These are accessible from anywhere as "X.objErrorProp" and "X.objSysProp".

P.S. UseTheForumNotEmail
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!