aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion about class creation inside a class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion about class creation inside a class." Watch "Confusion about class creation inside a class." New topic
Author

Confusion about class creation inside a class.

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hello,

I have a confusion about creating an object inside a class.
For example:

public class A {

int int_var = 10;
String str_var = "Java";
myClass myclass = new myClass();

public void someMethod () { ....}

}

In this class, the first two initializations are OK. I can create int_var and str_var, but not the myclass.

I'm a little confused as to what the exact reason for this is.
Particularly since both str_var and myclass are both OBJECT instances. What is the rule for what can be done and what not outside a method in a class.

Thank you very much.
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Hi below code works fine




what is MyClass in your code


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you mate.

myclass is, for example an 'inner class'. There is a restriction on that?
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
For example;

public class MyClass {

int int_var = 1234;
String str_int = "Java";
MyClass myclass = new MyClass();

//constructor
MyClass { System.out.print(str_int); }


void someMethod() { ...}

}

Seems I cant do this.
It compiles but at runtime gives errors.

That means I cannot have object instantiation outside a method?
The reason I can have a String is because it is static?
The constructor prints "Java" as expected.

Thank you.
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
...
MyClass myclass = new MyClass();

//constructor
class MyClass { System.out.print(str_int); } //where is the keyword class???

...
[ July 22, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
it is the constructor..
I do not need the the keyword 'class' do I?
JCG
Greenhorn

Joined: Mar 27, 2008
Posts: 2
class MyClass{

//This is constructor

MyClass(){
}

/*This is wrong
MyClass{} //() missed


*/

}
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
Maduranga ...t his is the correct declaration..

//outside class
public class MyClass {

int int_var = 1234;
String str_int = "Java";
// you are getting a n instace of the inner class
MyClass myclass = new MyClass();

//this ain't a constructor ... its a class definition!! Constructors are inside the their class defintion
// & so you require the keyword class
class MyClass {
// inner class Constructor!!!
MyClass(){}
//this System.out.print(str_int); should be inside some (inner)method
}

//outer class method
void someMethod() { ...}

}
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Milan,

I think I got it confused.
This is what I want to know.

public class MyClass {

int int_var = 1234;
String str_var = "Java";
MyClass C = new MyClass();

public void method_1() {
int_var = 5678; //changes the int_var of 'this'
C.int_var = 7777; //changes int_var of another MyClass object.
}

}

This does not compile because of MyClass C = new MyClass();
If this is to work I need to create the object inside method_1();

I need to know why I can instantiate a String object and not a Class object.. is it because String is static?

Thank you.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello Maduranga,

Your code compiles fine. What's the problem you are having?
Maybe you are getting a StackOverflowError. It happen if you try to instantiate a MyClass Object, because the object will instantiate a new MyClass Object, and so on..

Kind Regards,
Raphael Rabadan
[ July 22, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Raphael.

I think that is exactly what it is. Actually it is not a compile time error. Sorry. When I run the code I get a big list of errors. I think it is 'StackOverFlow'. Now I get it.

So I actually 'CAN' instantiate objects of the same class inside the class.
Thank you mate. Will check more on that.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
This maybe an obvious question.
A class can have instance variables and methods.

So it means I cannot do anything like 'System.out.print("Test");' ..?
When I do that I get a compile-time error saying '<identifier> expected'..

THank you.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello,

if you don't put the System.out.println("..."); inside a method you will get this error.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks mate.
got it now...
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Just checked by compiling.. wan to make sure..

I can have statements like:

int val=10;
int other_val = val + 10;
String s = "Java";

inside a class (not inside a method).

But I MUST have statement like System.out.print, while and for, inside methods.

Cheers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Confusion about class creation inside a class.