• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Constructor discussion.  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, could you help me out with below questions.

1) What happens to super() call to Object class constructor. I mean constructor whether it's no-arg or arg, executes parent to child. So Object being Super class of all, how Object class defines or execute it's no-arg or arg constructor.

2) void nameMe() { return; } ----> It is allowed as method return type is void so "return;" make sense. what about constructor? We know constructor have not return type, still we can write "return;" inside constructor like this.

class A{
   A() {
      return;
   }
}

question here is, why is that? "void" in constructor is not allowed but "return;" is allowed.
 
author & internet detective
Marshal
Posts: 38659
677
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) super() in Object is the last thing called. Even if you have a constructor, it will eventually call super() in Object. That's what creates the object

2) return is just like a statement that says "stop executing me". So the constructor knows you can't return a type. It' snot a good idea to have "return" in a constructor even if it is allowed though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!