Instantiable Classes: fields vs methods vs constructors

Instantiable classes are class (types) that are defined by a programmer to allow that or other programmers the ability to create instances (objects) of that data type.  Such types are sometimes referred to as composite data types.  Instantiable classes create a new type that stores multiple values as one entity.  If a programmer has defined a Book class, and a Student class, they may then create instances of those data types (classes) like this:

 

Book b = new Book("Java for Everyone","Cay Horstmann",589) ;

Student b = new Student("Suzy Q","900111222") ;

 

A class is considered instantiable, if it contains instance fields and instance methods.  See static vs instance (non-static) for more information about the static modifier.  A class with only static fields and methods would not generally be considered to be an instantiable class.

Fields

Fields represent the values that are needed to represent the new data type.

 

Methods

Methods represent the actions (verbs) that an object can do, or the messages that an object can respond to.

Constructors

Constructors are blocks of code that have the same name as the class and they contain code that is used to initialize the values of the instance fields of an object at the time that the object (instance) is created.  This is also when memory is allocated to the new object.

 

Putting It All Together into an instantiable class, a main class, and a utility class.

This example correctly places all of the above pieces into a few classes that can be used together to create Widgets and then print them out.  Widgets could be used in an inventory program or other.

Widget.java

WidgetMain.java

WidgetUtil.java

 

© 2014 Debra Deppeler, All rights reserved.