Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

It seems that whether a constructor declare as void or not isn't so important?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I dont know the difference between declare the constructor as void or not.
//######################-READ THE CODE####################//
public class TTT
{
public static void main(String argv[])
{
TTT t=new TTT();
System.out.println(" I am main");
}
protected void TTT() //*******you can replace with "protected TTT()" too*******//
{
System.out.println("in constructor");
}

}
//#########################################################//
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

void is also a type.Constructors can not declare as void b/c they does not return any thing.
Hassan.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
protected void TTT() --> this will be considered as a method not a constructor.
protected TTT() --> this is a constructor.
Constructors can't have a return type.
Vanitha.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vanitha is correct. A method that looks like a constructor but has a return type is not a constructor. It is just a regular method.
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You meant that the constructor always void cant have a return type.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, having a void return type and no return type are not the same thing. Constructors have no return type at all; they can not have a void return type, or else the compiler treats them as a normal method rather than a constructor.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
constructor has no return type. But it has implicit type i.e.class type in which it is defined.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic