Class Definition

     

You can have multiple class files in the same source (.java) file, but only one of them may be declared public.

 

 public class MyClass {     // class definition here } class AnotherClass {} 

Class Modifiers

You can add class modifiers to the declaration that changes the definition of the class.

public

This class modifier indicates that you want it to be available to classes other than just those in the package in which it is declared. That is, you'll let anybody use it. If your class does not include the public modifier, it can only be used by other classes within the same package.

final

To make your class so it cannot ever be subclassed, you include the final modifier in the class declaration. This example is from the standard API.

 

 public final class String {...} 

abstract

Declare an abstract class to indicate that its implementation is not complete (that you didn't write code to do the work in all of the methods). Only abstract classes may have abstract methods . For example, the following is illegal :

 

 public class MyClass { public abstract void someMethod(); } 

In the preceding example, you must either implement the method or declare the class abstract and implement the method in a subclass.

strictfp

This modifier indicates that your class will use the strict IEEE 754 standard for floating point (fp) variables, whether or not the methods or variables themselves are declared strictfp .

Declaring Methods

This example declares a method called setQuantity() that accepts a variable called qty of type int. Note that this is only something you can do in an abstract class or interface ”declare it without implementing it.

 

 public void setQuantity(int qty); 

This example declares a method called getQuantity() that returns a variable of type int .

 

 public int getQuantity(); 

Methods declared without an access modifier will have default access.

 

 void setQuantity(int qty); 

Calling Methods

Methods are called using dot notation against the object that defines the method. Your code has to account for the return type that the method declares. For example, if getQty returns an int , you call the method to set a variable of type int to the return value. Say that the getQty method returns an int with a value of 5:

 

 int theQty = inventory.getQty(); //theQty now has a value of 5 

You can also call a method directly to get the value without having to set its value into a variable. This is useful in constructors in particular: Say that we have an employee class that defines a constructor that accepts an int value for an ID and a String for a name , and a Manager class with a similar constructor:

 

 Employee employee = new Employee(999, "Kevin Bacon"); Manager manager = new Manager(employee.getID(), employee.getName()); 

You may also have a class that defines static methods ”that is, methods that do not require an instance of the class to operate . An example from the API follows :

 

 Collections.sort(myArrayList); 

Notice that we call the sort method directly on the Collections class. The Collections class happens to consist exclusively of static methods that work with collections data.

Overloading Methods

Overloading a method means defining multiple methods in the same class, each with the same name, each of which accepts a different argument list.

 

 public void calculateSquareRoot(int i); public void calculateSquareRoot(double d); 

The preceding calculateSquareRoot() method is said to be overloaded.

Overriding Methods

Overriding a method means defining a method in a subclass that has the same signature as the method of the same signature in the superclass. For example, the String class overrides the equals() method defined in its superclass Object. The following class definitions show this in action:

 

 public class Test { public void printQuestions(){ System.out.println("Multiple Choice..."); } public static void main(String[] args) { Test test = new EssayTest(); test.printQuestions(); } } class EssayTest extends Test { public void printQuestions(){ System.out.println("Essay..."); } } 

Executing this file prints "Essay" to the standard out.

Reading User Input from the Standard In

Sometimes, you want to accept user data at runtime from the console. The following code example demonstrates how to read user input from the standard in ( System.in ).

 

 //do imports and such public static void main(String[] args) {     System.out.println("Please say something ");     try {        BufferedReader is = new BufferedReader(                 new InputStreamReader(System.in));        String input;        while((input = is.readLine())!= null){             //consider doing something even more             //exciting here with the input             System.out.println("You wrote: " + input);           is.close();           System.exit(0);        }     } catch(IOException ioe){        System.err.println("Exception: " +                                 ioe.getMessage());     } } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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