• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

What is this?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors do not have a return type
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors can have public/private Access Specifiers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic