• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Object creation problem.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:

public class Hello
{
public Hello()
{
System.out.println("a");
}
public void test(){}
public static void main(String arg[])
{
Hello h = new Hello();
Hello h=new sub();// this is not compiling

}
class sub extends Hello
{
Hello h=new sub();
public sub()
{
super();
System.out.println("s");

}

}
}

Query:- Why we can't do "Hello h=new sub();" in the main method in this class?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the error message ? I think it's very clear about what the problem is.
If you don't understand the error message, post it here and someone will explain.
[ March 27, 2008: Message edited by: Joanne Neal ]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello h=new sub();// this is not compiling



You are declaring h again!
 
Ravinder Partap
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I forgot that while experimenting all the option I already had used variable h.
Still converting this variable "h" to "h1" in following line

Hello h=new sub();// this is not compiling

Make it as:-

Hello h1 = new sub();

Now still it doesnt gets compiled.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't need to make inner class, just try bring 'sub' class our of Hello and try it. like this:

public class Hello
{
public Hello()
{
System.out.println("a");
}
public void test(){}
public static void main(String arg[])
{
Hello h = new Hello();
Hello h=new sub();// this is not compiling

}
}

class sub extends Hello
{
Hello h=new sub();
public sub()
{
super();
System.out.println("s");

}

}
 
Kamlesh Kr
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if you need the inner class try this:
public class Hello {

public Hello() {
System.out.println("a");
}

public void test() {
}

public static void main(String[] arg) {
Hello h = new Hello();

Hello h1=h.new sub();// this is not compiling
}

class sub extends Hello {
Hello h = new sub();

public sub() {
super();
System.out.println("s");
}
}
}
 
Ravinder Partap
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kamlesh ! Your second answer cleared all my doubts.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic