• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object creation  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i create the object for simple class ?
public class simple {

}
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple simpler = new Simple();
Is this what you mean ?
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivas, try to compile this class? It gives compilation error, because access modifier is public.

public class simple {

Simple simpler = new Simple();

}
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java syntax is case-sensitive. So,

won't compile (take a look at the class name).

If we fix the code,

the code will compile. BUT this class will be useless. Any attempt to create an object of the type Simple will result in stack overflow error because there is an infinite recursion.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any executable statement should be in a method right ? Or in a block like static block. So how the following code gets executed. Am i missing something.


Hi Kri Shan can you just post the error you are getting in.
[ March 01, 2005: Message edited by: Srinivasa Raghavan ]
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class simple {

public simple() {} // constructor
public static void main(String args[]) {
simple sim = new simple();
sim.meth1(); // meth1() is not a static method
}

When i tried to create the object for simple within main... it gives compilation error?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kri shan In your code below, where you have defined the method "meth1()"

Try to define that method and compile it.



PS : post the exact error you are getting so that it 'll be easy to debug or solve the problem.

The code mentioned in the first post will compile.
In the above code a instance variable is declared & initilized. But you can't run the class because you dont have a main method.
[ March 02, 2005: Message edited by: Srinivasa Raghavan ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivasa Raghavan:
Any executable statement should be in a method right ? Or in a block like static block. So how the following code gets executed. Am i missing something.



You can also have initailizer statements for your class member variable (like int i = 5), so this code would compile and would result in an stack-overflow error.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel for pointing this out. Actually I have updated my recent post.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!