Creating Instances of class within its definition
Hi,
I am a newbie to Java and am doing a course on Java. In one my assignments it asks us to develop the following method for a Class called CraftStock:
"Define a new helper method void increaseStockCapacity() that creates a
new expanded array as follows. It should create a temporary instance of
CraftStock by using its single-argument constructor ?the value of the argument n is determined by the current size of the original craft stock array plus the
extraCapacity constant. All items in the original array must be copied from the
original array to the temporary instance of CraftStock. The array reference in the
original array instance of CraftStock must then be reassigned to the new
temporary array instance."
My question is - is this possible - can one create an instance of a class from within its definition? I can't find anyhting on this from the books.
Any help would be appreciated.
Thanks
Dev
[987 byte] By [
devsena] at [2007-10-2 21:04:28]

Hi,
Yes there's no problem in creating instances of a class from within the class.
e.g.
public class Example {
public Example createExample() {
return new Example();
}
}
Kaj
Ps. Yes, it's a very silly example.
kajbja at 2007-7-13 23:49:40 >

Yes.
A class can have a method (or whatever) that creates a new instance of itself (here it does it twice - once in a static method and once in an instance method):
public class SomeClass {
public void makeMe() {
SomeClass sc = new SomeClass():
. . .
}
public static void main(String[] args) {
SomeClass x = new SomeClass():
x.makeMe();
}
}
jbisha at 2007-7-13 23:49:40 >

in fact, this sort of behaviour is very common. it forms the basis of the Factory method pattern
Isn't there a caveat to all this?If one creates an instance of a class within its own definition, doesn't one face the potential danger of attempting to access some part of the object which has not yet been constructed?
> Isn't there a caveat to all this?
>
> If one creates an instance of a class within its own
> definition, doesn't one face the potential danger of
> attempting to access some part of the object which
> has not yet been constructed?
nope. why would it? we're creating a new instance of the class, from scratch. we're not accessing any part of the object, we're creating a new one. the fact that this is being done within the definition of the class is irrelevant. by the time anything comes to call on a static method of the class, the class is initialized already. remember, static methods don't have any dependency on a particular instance
sorry, that doesn't read very well, does it! what I mean is that the class itself is not the same thing as an instance of that class
Message was edited by:
georgemc
I admit, this code is crude and unlikely, but I was thinking:public class Forum
{
int member = 123;
Forum myself = new Forum();
public static void main(String[] args)
{
Forum object = new Forum();
System.out.println(object.myself.member);
}
}
This compiles but will not run.
> I admit, this code is crude and unlikely, but I was
> thinking:[code]public class Forum
> {
>int member = 123;
> Forum myself = new Forum();
>
>public static void main(String[] args)
> {
>Forum object = new Forum();
> System.out.println(object.myself.member);
>}
> code]This compiles but will not run.
But in this case the reason it "won't run" is because it is stuck in an infinite loop - not because of any partial construction. Creating a Forum object creates a Forum object which creates a Forum object...
jbisha at 2007-7-13 23:49:40 >

I see.I must be confusing two separate issues.I seem to recall a recent thread where I was told of the danger of accessing a partially constructed object.I can't find it now and don't recall the context in which that problem could arise.
> I see.
> I must be confusing two separate issues.
> I seem to recall a recent thread where I was told of
> the danger of accessing a partially constructed
> object.
> I can't find it now and don't recall the context in
> which that problem could arise.
It may have been about letting a reference leak out of the constructor before construction is complete: class Foo {
String name;
Foo() {
bar.baz(this);
name = "abc";
}
}
Now bar has a reference to this Foo, and it could start using it before the Foo's name has been set.
Or it might have been that thread where valjok thought it was unnecessarily restrictive that Java requires that the superclass' constructor completes before running the subclass' constructor.
jverda at 2007-7-13 23:49:40 >

> I see.
> I must be confusing two separate issues.
> I seem to recall a recent thread where I was told of
> the danger of accessing a partially constructed
> object.
> I can't find it now and don't recall the context in
> which that problem could arise.
Maybe something along the lines of this (just found it in Google)?
http://nat.truemesh.com/archives/000222.html
jbisha at 2007-7-13 23:49:40 >

> Now bar has a reference to this FooYes, I think that's it.I'm assuming that "bar" is yet another Foo type object and "baz" is a method somewhere else in the Foo class - which, in turn, has a Foo object as a parameter?
> > Now bar has a reference to this Foo
>
> Yes, I think that's it.
> I'm assuming that "bar" is yet another Foo type
> object
bar could be any class, not necessarily a Foo.
> and "baz" is a method somewhere else in the
> Foo class - which, in turn, has a Foo object as a
> parameter?
A method in whatever class bar is, but yes, it takes a Foo as a parameter.
jverda at 2007-7-13 23:49:40 >

OK.I think I've got it now.Thank you very large.Have a nice Memorial Day. (How do you say that in Spanish?)
> (How do you say that in> Spanish?)Is that addressed at me or at the forum in general? I don't speako el Spanisho.
> Is that addressed at me or at the forum in general?The forum.> I don't speako el Spanisho.Neither do I.Maybe the forum should have a "Press 1 for . . . "Message was edited by: ValentineSmith
> . . . letting a reference leak out of the constructor before construction is complete:class Foo {
String name;
Foo() {
bar.baz(this);
name = "abc";
}
}
> Now bar has a reference to this Foo, and it could start using it before the Foo's name has been set.
So, I've been trying to write a little program that shows this.
I've had no success.
I keep going into an infinite loop.
Can anyone complete the above code to demonstrate what 'jverd' is talking about.
public class Leak {
final String name;
public static void main(String[] args) throws Exception {
new Leak();
}
Leak() {
LeakHelper helper = new LeakHelper();
helper.help(this);
name = "abc";
helper.help(this);
}
}
class LeakHelper {
void help(Leak leak) {
System.out.println("leak's final name is " + leak.name);
}
}
leak's final name is null
leak's final name is abc