| ||
| Classes Introduction A class is declared by the use of class keyword. The general form of a class is given above. The class with the main method initiates execution of a program. There can be classes created without main method. These classes will act as a user defined data types or the type definition. | ||
| A Simple class Here is a class called as Box that defines three variables : length, width and height. Currently class box does not contains any methods. In this case we have created a new data type called Box. | ||
| Constructors Constructor name is class name. A constructors must have the same name as the class its in. Default constructor. | ||
| Parameterised Constructors Constructors and methods differ in three aspects of the signature: modifiers, return type, and name. Like methods, constructors can have any of the access modifiers: public, protected, private, or none. Unlike methods, constructors can take only access modifiers. Therefore, constructors cannot be abstract, final, native, static, or synchronized. | ||
| Garbage Collection The name "garbage collection" implies that objects no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies can be recycled so that the space is made available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. | ||
| The finalise() method Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected. Your class can provide for its finalization simply by defining and implementing a method in your class named finalize(). Your finalize() method must be declared as follows: | ||
| Overloading Methods Overloading is used in the standard Java classes. The System.out.println method accepts multiple parameter lists. This sample code snippet is valid: | ||
| Recursion It is the central Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our method will never finish. This might be true is some cases, but in practise we can check to see if a certain condition is true and in that case exit (return from) our methodclass Factorial { int fact(int n) { int result; if ( n ==1) return 1; result = fact (n-1) * n; return result; } } public class Recursion { public static void main (String args[]) { Factorial f =new Factorial(); System.out.println(“Factorial of 3 is “ + f.fact(3)); System.out.println(“Factorial of 3 is “ + f.fact(4)); System.out.println(“Factorial of 3 is “ + f.fact(5)); } } The output from this program is shown here: Factorial of 3 is 6 Factorial of 4 is 24 Factorial of 5 is 120 | ||
| The final Keyword A variable can be declared as final. Doing so prevents its contents from being modified. This means we have to initialize the variable when it is declared. Keyword final in java is similar to "const" in c / c++. The value of final variables will remain same through out the application.For example: final int FILE_NEW = 1; final int FILE_OPEN = 2; final int FILE_SAVE = 3; final int FILE_SAVEAS = 4; final int FILE_QUIT = 5; | ||
| Arrays Revisited Arrays are aslo implemented as objects. Hence there is a special array attribute to tell the size of the array, that is teh number of elements that an array can hold is found in its length instance variable. All arrays have this variabe, and it will always hold the size of the array. Here is a program that demonstrates this property.public class Length { int l; public static void main(String args[]){ int n1[] = new int[10]; int n2[] = {3, 5, 7, 1, 8, 99, 44, -10); int n3[] = { 4, 3, 2, 1); System.out.println(“Length of n1 is “ + n1.length); System.out.println(“Length of n2 is “ + n1.length); System.out.println(“Length of n3 is “ + n1.length); } The output from this program is shown here: Length of n1 is 10 Length of n2 is 8 Length of n3 is 4 |
Search Java Programs
Showing posts with label 5-Classes-and-Methods. Show all posts
Showing posts with label 5-Classes-and-Methods. Show all posts
Thursday, January 7, 2010
Classes and Methods
Classes and Methods
Methods are subroutines or functions used to perform a task with or modify an object. Since classes are the base of all java structures all functions must be included in a class. There are three levels of visibility for methods, public, private, and protected. Public methods are accessible from anywhere in the program, private methods are only accessible from within the class, and protected methods are available inside the class and any subclasses in the same package. If no access is specified class and package have access but not subclasses. A method must declare the type of return value or void for no return value.
Classes may also contain class wide variables with the same public, private, protected attributes. It is usually considered best practice to set the class variables as private and use public getter and setter routines to access the variable. This is called encapsulation.Include GetterSetterEx.java
class GetterSetterEx {
private int i = 0;
private String s = "string";
public int getI() {
return(this.i);
}
public void setI(int i) {
this.i = i;
}
public String getS() {
return(s);
}
public boolean isSLongerThanI() {
if (s.length() > i) {
return(true);
} else {
return(false);
}
}
public GetterSetterEx(int i, String s) {
this.i = i;
this.s = s;
}
}
This class simply demonstrates how to create private class variables and getter/setter methods for each. The variables are declared as private and can only be accessed from this class. When using this class in conjunction with any other class you must use the get and set methods for each variable. In addition to the getters and setters we added another simple method to determine if the string is longer than the value of the integer.
Also notice the final method in the class, and how it has the same name as the class. This is called the constructor and is called whenever the class is initialized. This method expects 2 values to be passed at initialization. The constructor method should always be declared public with no return type or void declaration. Multiple constructors can exist with different parameter types that handle initialization differently.
Importing Packages
Java uses the import command to include external packages to extend the functionality of your application. The Java Runtime Environment (JRE) includes a large set of libraries to build your application with ranging from network socket connections to graphical user interface (GUI) building packages. Before a package or class, such as the one we previously created can be used, they must be imported. The import statement comes at the beginning of the .java file and should be outside the class declaration.
Subscribe to:
Posts (Atom)