Showing posts with label Reserved keywords. Show all posts
Showing posts with label Reserved keywords. Show all posts

Wednesday, February 20, 2013

Class declaration

Every Java class begins with a class declaration, as follows:

public class MyClass {

}

public and class are reserved Java keywords and you may not use them to name entities in your own library files.

MyClass is the name of the class - this can be anything you like, however:
  • must not contain spaces - rule
  • should start with an uppercase letter - convention
  • should not start with a number - convention
  • where there are multiple words in the class name, the first letter of each word should be capitalised (also known as Camel-Case) - convention
Note: A rule applies to language syntax and cannot be broken. Convention applies to common standards and these can be broken.

Every class declaration includes a set of curly braces - one that opens a block, and one that closes the block. All code for the class must be contained within this set or the code will fail.

Another example:

public class NetworkUser {

}

This class is called NetworkUser.
 

Wednesday, July 25, 2012

protected keyword


"Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/JgfUgH )

The super keyword


"Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/PYKCvR )

Sunday, May 27, 2012

What is a Constant? Final keyword

DOWNLOAD THE CODE: FinalValuePkg

We can store a value in a constant. Very similar to a variable except that a constant has a constant value. That is, the stored value is never changed as a result of processing.

Constants are used where a value should never be changed such as a maximum, minimum, number of hours in a day, days in a week, etc.

A constant begins with the keyword final. Just as with a variable, a type & identifier must be given.


The compiler will not allow any processing statements to change the values of these constants.


UML Class Diagram Notation
The UML Class Diagram notation for a constant (final) is as below:



CIRCLE_WIDTH and CIRCLE_HEIGHT are defined using uppercase and have each been assigned a value.

"Java Syntax: Declaring and Using Constants in Java" ( http://bit.ly/JmgF5S )

Here's a cat:


Friday, May 18, 2012

The 'static' keyword


Download the code


Static variables & methods are associated with the class rather than an instance of an object of that class.

Usually, the data contained within an object, belongs only to that instance.

If you want all instances of a class to share some data, use a static variable. A static variable belongs to the class, rather than an instance the class (object).

The example below uses a static variable to keep count of the number of Widget objects that have been created.

The Widget class contains a single static variable called _numberOfWidgets. No matter how many instances of Widget are created, there will be a single value associated with the _numberOfWidgets variable.

A static method is provided to set the value of _numberOfWidgets and a static method is provided to get the value of _numberOfWidgets. In this example, the constructor calls the set method and in turn, the set method increments the value.


In the TestWidgetClass below,  a number of Widget instances are created. Note that the call to the get_numberOfWidgets method is made by referencing the class directly, rather than by instance of the class.



And the output is:




"Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/KmbYd7 )

In a UML class diagram, Static variables & methods are represented with an underline.



Here's a cat to keep you calm:


Tuesday, April 24, 2012

Constructors and instantiation

"Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/I67fRB )

Every class has at least one Constructor. This is a special method that prepares the object for use. It has the same name as the class.


A class can have multiple constructors - this is known as method overloading.

The constructor is always the first method called on an object. Think of it as an 'on-switch'. When we instantiate an object of a class, we call it's constructor using the new keyword:

Syntax:
Type identifier = new Type(parameters, if any);

Example:
Telephone myTelephone = new Telephone();


In UML notation, the constructor is always referred to as new(). This is because UML is language independent and not Java specific. Different programming languages have different rules for naming a constructor.