There are two parts of a class
- Fields
- These are variables also called as data members that store data items.
- These data items differ from one object to another.
- Fields can be of any of the primitive types, or they can be references to objects of any class type, including the one that you are defining.
- There are two types of fields in a class
- The field is associated with a class and is shared by all the objects of the class.
- There is only one copy of each of these fields and they exist even if no objects of the class have been created.
- This kind of variable is referred to as Class variable because the field belongs to the class and not to any object.
- A given class will have only one copy of each of its static fields or class variables.
- Each of this is shared between all the objects of the class.
- Each class variable exists even if no objects of the class have been created.
- Class variables belong to a class and they can be referred by any object or class method,not just methods belonging to the instance of that class.
- If the value of the static field is changed,the new value is available equally to the all objects of the class.
- A static field is declared using the keyword static preceding the type name.
- The other kind of field in the class is associated with each object uniquely.
- Each instance of the class will have its own copy of each of these fields,each with its own value assigned.
- These are called as non static fields or instance variables because we specify them without using a static keyword.
- Each object of the class has its own copy of each of these non static fields.
- Each object will have its own values for each of the instance variables.
- An instance variable is declared within a class definition in a usual way with a type name and a variable name, and can have an initial value specified.
- Changing value in one object does not impact values in other objects.
- Methods
- These define the operations you can perform for the class
- They determine what you can do to or with objects of the class.
- Methods operate on fields the variables of the class.
- Methods provide the actions that can be carried out using the variables specified in the class definition.
- There are two types of methods
- Instance methods
- Instance methods can be executed only in relation to a particular object.
- Class methods
- Class methods can be executed even if no objects of a class exist.
- Class methods are declared using static keyword so they are also called as static methods
- Static methods cannot refer to the instance variables because they can be executed when even no instance is available.
- The main method is static because when application starts execution no objects exist.
- When we create a class for utility methods we use static methods inside the class for example Math class in Java.
- Static members of a class can be accessed from outside separately.
- To access a static member use a class name followed by a period,followed by the member name.
- For class methods we also need to supply parentheses enclosing any arguments to the method after method name.
- The period is also called as dot operator.
- An Example of how we call static methods is as follows
double rootPi=Math.abs(Math.PI)
- You can also access static members using objects of the class.
- We can import static members of a class using import statement.
- Instance variables and methods can only be called using object reference.
- A class is defined using the keyword class followed by the name of the class,followed by braces which enclose its definition.
- A class can be named in similar fashion as an identifier.
- By convention class names should began with a capital letter
- An access provider is bound to a class to handle access of the class within a package.
- We use final keywords in a class to make constants values of such variables that cant be changed.
- A method is a self contained block of code that has a name, and has a property i.e it is reusable.
- We execute a method by calling its name and the method may or may not return a value when its execution finishes.
- If we do not capture the value returned by a function then it is discarded.
- We specify a return type of a method we define the type of value that will be returned by method when we execute the same.
- To define a method that does not return a value we define its return type as void.
- The access identifier of a method identifies the access of a method at different levels such as global,within package etc.
- The parameters to a method appear in its definition between the parenthesis, following the method name. These specify what information is to be passed to method when we execute it.
- The parameters are also called as arguments.
- The parameters are optional.
- To return a value from a method when its execution is complete we can use a return statement.
- After executing the return statement the program no longer executes the current function it returns back to the point from where function was called.
- The value returned can be only be of type as defined in declaration of the method.
- Methods declared with a return type other than void must always finish by executing a return statement that returns a value of appropriate type.
- The parameters list appears between the parenthesis following a method name.
- This specifies the type of each value that can be passed as an argument to the method, and the variable name that is used in the body of the method.
- A parameter has a name and a type and appears in the parameter list in the destination of a method.
- A parameter defines the type of value that can be passed to the method when it is called.
- An argument is a value that is passed to a method when it is executed and the value of the argument is referenced by the parameter name during the execution of the method.
- In java all values to a method are passed by value.
- A copy of the value is made, and it is the copy that is passed to the method and referenced through the parameter name, not original value.
- This implies that if we use a variable as a argument.The method cannot modify the value of variable.
- Pass by value applies to all types of arguments but the effect for objects is different from that of the primitive types.
- We can change the object because a variable of class type contains reference to object and not the object itself.
- Thus when we use a variable of a class type, as an argument to a method, a copy of a reference to the object is passed to the method and not copy of object.
- Since copy of a reference refers to the same object.The parameter name used in the body of a method will refer to original object that was passed as the argument.
- You can specify any of the parameters for a method as final.
- These parameters cannot be modified anywhere in the program.
- Since we pass by value for basic types final works perfectly only when it is applied to parameters that are references to class objects.
- Specifying a parameter of a class as final is of limited value.It prevents accidental modification of the object reference that is passed to the method,but it does not prevent modification of the object itself.
Class Methods
- A class method is defined by adding a static keyword to its definition.
- We cannot refer to any of the instance variables in a static method because a static method can be executed when no objects of a class have been created, and therefore no instance variables exist.
- An instance method can access any of the data members of the class, just by using the appropriate name.
- This variable in any instance refers to the current object for which the method is being called.
- The compiler uses this implicitly when a method refers to an instance variable of a class.
- Since only one copy of instance method of a class exists the "this" variable sets its reference to a particular object to which it is being applied.
- To initialize the data members either we can initialize them to a default value directly as follows
static final double PI=3.14;
- We can also initialize non static data members using a constructor.
- An initialization block is a block of code between braces that is executed before an object of the class is created.
- There are two kinds of initialization blocks
- A static initialization block is a block defined using the keyword static and is executed once when the class is loaded.
- A static initialization block can initialize only static members of the class.
- A non static initialization block is executed for each object that is created and thus can initialize instance variables of a class.
- Example
static{ Systm.out.println("Running initialization block") }
- When we create an object of a class, a special kind of method called as constructor is invoked.
- If we don't define any constructor the compiler provides a default constructor which is invoked
- The default constructor has no arguments it is also called as a no-arg constructor.
- The primary purpose of a constructor is to provide you with the means of initializing the instance variables of the class.
- A constructor never returns a value and as such we do not specify any return type to the same.
- A constructor always has a same name as of the class.
The Default Constructor
- The default constructor is provided when we do not provide any constructor to a class.
- The object created by default constructor will have fields with their default value sets.
- Any non static initialization blocks will be executed each time any constructor executes,immediately before the execution of the code in the body of constructor.
- A class may have more than one constructors with different parameters.So in case we have supplied a constructor with parameter and we require a default constructor we need to specify one.
- We declare variable of type "Shapes" with following statement
Shapes square;
- This creates a variable square which contains reference to the object of type Shapes.
- To create an object of the class we must us the new keyword followed by a call to a constructor.
Shapes square = new Shapes();
- Objects in a method are passed by reference.
- The copy of reference to object is passed instead of complete object.
- The changes in object are maintained even after function call.
- This only applies to objects.If you pass a variable of type int or double to a method, a copy of value is passed.
- Such a value if modified in method does not impact the original value.
- The life time of an object is determined by number of variables that refer to it.
- If all the variables go out of scope then the object dies.
- If we set the variable to NULL and that variable is only one referencing the object then also the object dies.
- The process of disposing of dead objects is called as Garbage Collection.
- Garbage Collection is automatic in Java.However it can also be run manually if we are using large objects to free up space using
- System.gc();
public class twoDshapes {
static double PIE=3.14;
double length;
double breadth;
double thirdside;
final static int[] VALUES=new int[10];
//Initialization Block
static
{
System.out.println("Running Initialization Block");
for(int i=0; i<VALUES.length; i++)
{
VALUES[i] = (int)(100.0*Math.random());
}
}
public twoDshapes(double length,double breadth,double thirdside)
{
this.length=length;
this.breadth=breadth;
this.thirdside=thirdside;
}
void listValues()
{
for(int value : VALUES)
{
System.out.print(" "+value);
}
}
protected double Area()
{
return 0;
}
public double areaTriangle()
{
double S=this.triangleS();
double Area=Math.sqrt(S*(S-length)*(S-breadth)*(S-thirdside));
return Area;
}
private void incrementSides(twoDshapes s)
{
s.length++;
s.breadth++;
s.thirdside++;
}
private double triangleS()
{
double s=(length+breadth+thirdside)/2;
return s;
}
public static void main(String args[])
{
twoDshapes t1=new twoDshapes(20,20,10);
t1.listValues();
System.out.println("Incremented Sides : "+t1.length+" "+t1.breadth+" "+t1.thirdside);
t1.incrementSides(t1);
System.out.println("Incremented Sides : "+t1.length+" "+t1.breadth+" "+t1.thirdside);
}
}
s
No comments:
Post a Comment