Home arrow Java & J2EE arrow Page 2 - Java Classes

How to Declare a Class - Java

Well it's been a while since we discussed Java and I graced these pages with its complicated beauty (for an example of complicated beauty, take a peek at Hillary Swank, who is pretty in a well...complicated sort of way). In this new series of articles, we will be discussing Classes.

TABLE OF CONTENTS:
  1. Java Classes
  2. How to Declare a Class
  3. Using Public Methods to Obtain Private Fields
  4. Naming Conventions for Our Pal, the Method
  5. Creating Objects with Constructors
By: James Payne
Rating: starstarstarstarstar / 20
January 09, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are several ways to define your classes. The simplest would be:


class YourClass {

declare your field, constructor, and methods here

}

You can also define them in a more complex manner: 

class YourClass extends YourSuperClass implements SomeInterface {

declare your field, constructor, and methods here

}


Here is a list of components class declarations can contain:

  • Public, Private, and other modifiers.

  • Class Name.

  • Superclass name, if applicable with the keyword extends before it.

  • The keyword implements followed by a list of interfaces (separated by commas).

  • Body of the class, encapsulated with braces{}.

Variables in Classes

We discussed variables and data types in a previous article, but they need a little further discussion here, as they relate to classes.

Variables in classes are known as member variables and are called fields. They are composed of a modifier (such as public or private), a type, and a name. An example would be:


public int myEnormousIQ;


In the above example, the public portion of our declaration is known as an access modifier. It determines which classes have access to the member field. There are several access modifiers: public, protected, package, and private.

  • Public: can be referenced from anywhere in the application by the class in which it is defined.

  • Package: can be referenced by only the same package through the class in which it was defined.

  • Private: can only be referenced by the class in which it was defined.

  • Protected: can be referenced by its class and subclass and from its package.



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: