Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about class creation inside a class.

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi below code works fine




what is MyClass in your code
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you mate.

myclass is, for example an 'inner class'. There is a restriction on that?
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
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 ]
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is the constructor..
I do not need the the keyword 'class' do I?
 
JCG
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyClass{

//This is constructor

MyClass(){
}

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


*/

}
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

if you don't put the System.out.println("..."); inside a method you will get this error.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate.
got it now...
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic