This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.2 Java3002: creating and manipulating turtles and pictures in a  (Page 5/12)

The definition of the class named Prob01Runner begins in Listing 2 .

Listing 2 . Beginning of the class named Prob01Runner.
class Prob01Runner{ //Instantiate the World and Turtle objects.private World mars = new World(300,274); private Turtle joe = new Turtle(mars);private Turtle sue = new Turtle(mars);

No access modifier

Note that this class definition does not have an access modifier such as public , private , or protected . This puts it in package-private access category.

The lack of an access modifier causes a class to be put in the package-private access category. A class with package-private access can be accessed by code that is stored inthe same package and cannot be accessed by code stored in other packages.

Three private variables

The last three statements in Listing 2 declare three private variables. Because these variables are declared private , they can only be accessed by code contained in methods defined inside the class.

They are also accessible by code contained in methods defined in classes defined inside the class, but that is beyond the scope of this module.

Three private instance variables

These variables are also instance variables as opposed to class variables . (We will discuss class variables in a future module.)

Because they are instance variables, they belong to an object instantiated from the class. (An object is an instance of a class.) Even if the variables were public, they could only be accessed by first gaining access to the object to which they belong.

Multiple instances of the class

If you instantiate multiple objects of this same class (create multiple instances which you often do) , each object will encapsulate the same set of the three private instance variables shown in Listing 2 . Instance variables have the same name but may have different values in the different objects.

Three private instance reference variables

The three variables declared in Listing 2 are also reference variables (as opposed to primitive variables) . This means that they are capable of storing references to objects as opposed to simply being able to store primitive values of the following eight types:

  • char
  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean

Primitive variables can only store primitive values of the types in the above list.

Classes, classes, and more classes

A Java application consists almost exclusively of objects. Objects are instances of classes. Therefore, class definitions must exist before objects can exist.

The true power of Java

The Java programming language is small and compact. The true power of Java lies in its libraries of predefined classes.

The Java standard edition development kit and runtime engine available from Oracle contains a library consisting of thousands of predefined classes. Other class libraries containing thousands of classes are available fromOracle in the enterprise edition and the micro edition.

Non-standard class libraries

In some cases, you or your company may create your own class libraries and/or obtain class libraries from other sources such as the Ericson class library that we are using in this module.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.