Learning Java
Custom Search

Previous Next

Java Classes

In the following diagram, we have the class called Roses which gives generic information about what a rose is (data) and what a rose does (methods). The data in the Roses class includes color, species, and height. The methods in the class are Blooms and Wilts.

The object called MyChinensis, is instantiated (created) from the Roses class. MyChinensis is said to be an instance of the class, Roses.

    Roses MyChinensis = new Roses();

The new keyword creates the object or instance. The first "Roses" tells you that MyChinensis is of the class Roses. The second "Roses()" refers to the constructor (a method with the same name as the class).

Below you see that the method Blooms is called to act on the object, MyChinensis, with seasons = 2.

Notice the format of a class. It is identified by the keyword class and the brackets { } are needed to enclose the data and the methods.

class classname {

	data

	methods
}

Also, notice the format of the method. It must be followed by parentheses ( ) even if they are empty. Also, it's body must be enclosed with brackets { }.

class classname {

	data

	void methodname(any parameters) {
		do something;
	}
}

The new operator is used to create an object from the class.

classname objectname = new constructor(parameters);

To reference the data or the method of an object, you must include the object name and the data or method separated by a period. The method reference must include the parentheses.

objectname.data
objectname.methodname( )

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved