Is this a same thing? If not, what is the difference?
public class WhatEver{
SomeClass cl = new SomeClass();
//constructor
public WhatEver();
}
public class WhatEver{
//constructor
public WhatEver() {
SomeClass cl = new SomeClass();
}
}
Loading
Kevin AungPosted Jan 14, 2011, 7:38 PM
In the second one, cl is only available to the scope of the constructor so other methods won't be able to access it.
What I usually do is I declare the object as a member variable and instantiate it with "new" in the constructor. This way, you could instantiate for different constructors
public class classA
{
private classB m_myClass;
public classA()
{
m_myClass = new classB();
}
public classA(int Value)
{
m_myClass = new classB(Value);
}
}