• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is this?  RSS feed

 
Shawn Lo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Foof {
final int whuffie;

Foof(){
whuffie = 42;
}

}

The Foof() in line 4 looks strange. I know it cannot be a method, because it does not have public/private, it does not have void or non-void.
So, what is it?

Thanks you.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Methods do not have to have a visibility modifier. But this is not a method, it is a constructor, that is called when you want to create an object of this type.

Foof foof = new Foof();//calls the constructor and sets the reference foof to the new object
[ August 12, 2006: Message edited by: Rusty Shackleford ]
 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors do not have a return type
 
Chris Everty
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it cannot be a method, because it does not have public/private,
If you leave out a visibility modifier it has default that in effect means that the constructor is only package wide visible.


So every other class that belongs to the package "com.myname.mycoolpackage" may access the constructor "Foof", but also the attribute "whuffie". Every class outside "com.myname.mycoolpackage" cannot see the constructor and also not "whuffie".
 
paramu iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors can have public/private Access Specifiers.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!