• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Object creation

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i create the object for simple class ?
public class simple {

}
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple simpler = new Simple();
Is this what you mean ?
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joel for pointing this out. Actually I have updated my recent post.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic