3.2 DEFINING A CLASS IN JAVA


3.2 DEFINING A CLASS IN JAVA

Paralleling our class definitions for C++ in the previous section, here is a simple example of a Java class:

      class User {          private String name;          private int age;      } 

As for C++, we define the class User with two data members, name and age. As mentioned before, these are usually referred to as members, data members, or fields. The string type in Java is named String—the data type we use for the member name above.[4] Note that, unlike C++, a class definition in Java does not need a terminating semicolon.

Whereas for C++, leaving the access control modifier unmentioned meant that the data members were in the private section of the class. For achieving the same effect in Java, the modifier private must be made explicit in the manner shown.

As was the case with the C++ example of the previous section, in order to create objects from a class, the class needs a constructor. Here is a more useful definition of the User class in Java with a constructor included:

      class User {           private String name;           private int age;           public User(String str, int yy) {name = str; age = yy;}   //(A)      } 

The access control modifier public in line (A) serves the same purpose as it did for C++—it allows the rest of your program to create objects of type User. As before, no return type is specified for the constructor.

Now that we have available to us a constructor for the class User, we can create objects of this type by invoking the following form

      User u = new User( "Zaphod", 119 ); 

The invocation on the right creates a new object of type User and then returns a reference to the newly created object. Subsequently, the assignment operation causes the variable u to hold this reference.

We will now include in the class definition a print function:

      class User {           private String name;           private int age;           public User( String str, int yy ) { name = str; age = yy; }           public void print() {                                    //(B)                System.out.println( "name: " + name + " age: " + age );           }      } 

In the previous chapter, we briefly alluded to the Java method System.out.println for displaying information on your terminal.

As was the case with C++, a member function such as print() in line (B) above is invoked on an object, as in the following example:

      User u = new User( "Zaphod", 119 );      u.print();                   // name: Zaphod age: 119        //(C) 

where ‘.’ in line (C) is the member access operator. This is the only operator available for member access in Java.

Here is a working Java program that does the same thing as the C++ programs of the previous section:

 
//User.java class User { //(D) private String name; private int age; public User( String str, int yy ) { name = str; age = yy; } public void print() { System.out.println( "name: " + name + " age: " + age ); } } class Test { //(E) public static void main( String[] args ) { User u = new User ("Zaphod", 23 ); u.print(); } }

Note that we now have two classes, in lines (D) and (E), defined in the same file called User.Java. In keeping with our explanation in the last chapter, we compile this file by using the invocation

      javac User.java 

The compilation will deposit the bytecode for the classes User and Test in the files

      User.class 

and

      Test.class 

Of the two classes, only Test is executable since it contains main. We execute the class by

      java Test 

[4]Java also provides the StringBuffer type for strings that need to be modified after they are created. Further details on the String and the StringBuffer types are in the next chapter.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net