advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Articles : JavaBoutique's Introduction to Java :
Java Classes :

Contents
Classess
Abstract Classes and Interfaces
Methods
Overloading
Inheritance
Examples
Defining a Class
Instantiating and Using an Object

Defining a Class

So right after the "public" keyword, we see the "class" keyword. The class keyword is used to specify that the following statement block defines a class. The class can be used to instantiate an object that is defined by the class. In our example case, we are defining a class called "Announcer".

By the way, a class is usually defined in a ".java" file that is compiled into bytecode by a compiler into a ".class" file. Typically, your .java and .class filenames will be equivalent to the class name. Thus, the Announcer class would be stored in a file called Announcer.java and be compiled into a file named Announcer.class.

Once the class is specified, the class is defined. To define a class you'll remember, you simply define its properties and methods. In our case, we have one property called _announcements and several methods including: Announcer(), Announcer(), setAnnouncement(), getAnnouncement(), and printAnnouncement()


Construction

Hey wait a minute...what is the Announcer() method and why are there two of them?

Well, the Announcer() method is a very special method called a "Constructor". The constructor method, that always has the same name as the class file, is used to "construct" an actual object out of the class. When an object is instantiated this method is called to initialize the object. Once it is called, it will never be called again however. So you should only put initialization code here.

So why are there two of them? Well this is an example of polymorphism. The difference between the two versions of Announcer() is that one of them takes no arguments and the other one takes a single String as an argument. In the first case, an Announcer object will be created with the announcement of "Hello Cyberspace!" and in the second case, it will be created with some other phrase determined by the object that instantiated the Announcer!


Developing an API

After the definition of the constructors, you'll notice the methods printAnnouncement(), getAnnouncement() and setAnnouncement(). These methods represent the public API of this class and can be called upon by other objects. They allow other objects in the "object space" to work with the Announcer object. In the case of the printAnnouncement and setAnnouncement() outside objects are given the ability to ask the Announcer object to do things. In the case of the getAnnouncement, outside objects are able to ask the Announcer for some piece of data. As you can see, if a method is required to return some value, the value of the return type is specified in the definition of the method and the method is concluded by returning a value of that type. In this case, the getAnnouncement() method returns the string contained in the variable _announcement.

NEXT


Selena Sol contributes to the JavaBoutique's Introduction to Java. Selena curently works for Barclays Capital in London, one of the leading global investment banks in Europe and has worked as a software developer for the National Center for Human Genome research, Microline Software, Neuron Data, and Electric Eye in Singapore. Selena is perhaps best-known for creating the Public Domain Web Script Archive (Extropia) and writing several books on Web Programming (Perl, CGI, Java).
Email: selena@extropia.com

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs