This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initilization of variable  RSS feed

 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class example{

private int d=9;
public void hye(){

}
}

Is it ok syntatically ?
then what is the need of constructor?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To find out if it is ok syntactically just try and compile it. If the syntax is wrong, the compiler will not be shy in telling you.

In this case you don't need to explicitly define a constructor, if you don't a default constructor will be provided. It is generally considered better to do this:


But in this simple case it doesn't really matter. If you don't need to explicity set class level variables using the default constructor is fine, which sets the variables to 0 value, false or null depending on the type.

If you create any constructor, even if it is not the same signature as the default constructor(ie public NameOfClass()), the compiler will not provide it for you.
[ August 16, 2006: Message edited by: Rusty Shackleford ]
 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for reply ,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!