Skip to main content

What are Objects In Java Programming

 Java Programming Objects
  •  Object name in java is a reference
  •  References are used to store address 
  • All objects created in heap with new statement
  •  There is no delete statement in java
  • Deletion of object will be handled by garbage collector
  • A special value null can be assigned
  • Always passed by reference

Object Example
Let Student is a class
Student t; //here t is a reference variable

t=null;     // Assigning a reference to null 

// t=NULL  Error here as  Java  is case sensitive null and NULL are different

t = new Student(); // created in heap
- Alias name can created
for example
Student y=t; // here y and t referring to same object in heap


Parameter Passing
  •  Primitives by value
  •  Objects by reference
Object as parameter parameter passing by reference
Example 1

class Car{
String make;
String getMake() { return make;}
void setMake(String m){make=m;}

}
class ChangeCarMake{
 static void changeMake(Car p){
  p.setMake("Suzuki");
 }
}
class Test {
public static void main(String o[]){
Car c1=new Car();
c1.setMake("Honda");
ChangeCarMake.changeMake(c1); // passing a reference of class Car System.out.println(c1.getMake());

}
}
output
Suzuki


A program which  Exchange Marks
   
 class Student {
  String name;
  int marks;
  Student ( ) {  }
  Student (String n, int m ){name=n; marks=m;}
  String getName() {return name;}
  int getMarks() {return marks;}
  void setName(String n) {name=n;}
  void setMarks(int m) { marks=m;}
}

  class Test {
static void  exchangeMarks(Student a, Student b){
  int t=a.getMarks();
  a.setMarks(b.getMarks());
  b.setMarks(t);

 }
  public static void main(String o[]){
  Student s1=new Student("Amir",20);
  Student s2=new Student("Imran",90);
  exchangeMarks(s1,s2);
System.out.println(s1.getName() + "  "+ s1.getMarks()); 
System.out.println(s2.getName() + "  "+ s2.getMarks()); 
  }
  }

Comments

Popular posts from this blog

Generalization in java with examples

      Generalization basic points  Type/Subtype Relationship  "IS A " Relationship  One class has many subclasses   General class can have many Special classes as  subclasses  General names can be used to Address special names  Top to bottom hierarchy goes towards specialization  Bottom to top Hierarchy goes towards generalization e.g         Soap     Bath Soap and Washing Soap is a subclass of Soap e.g COldDrink   Pepsi      Team -Cold Drink is a general class -Pepsi and team are special classes e.g    Person Teacher       Student e.g            Car     Sedan           Hatchback Honda   Suzuki     Suzuki   Toyota civic        Liana      Alto      vitz ..........................................

Aggregation in java with example codes

What is aggregation?   "Have a" or "May have" relationship  Aggregation is special type of association  Life time of Component object is independent from its  container object E.g A school has one student class  Student  { String name; Student  () { name= null ;  } Student  ( String n ) { name=n;  } void  setName ( String n ) { name=n; } String getName () {  return  name; } } class  School { Student  st; School  (  ) {  st= null ; } School  ( Student s ){ st=s; } void  studentName (){    System.out.println ( st.getName ()) ; } } class  Post { public static  void  main  ( String a []){ Student st= new  Student ( "Imran" ) ; School s=  new  School ( st ) ; s.studentName () ; } } Output Hello ...

Life Time of Objects in Java with code Examples

Lifetime Concept in Java Programming Time between allocation and de-allocation of memory Life time depends on block i.e { }  life time starts when  declaration statement elaborated and expires at end of block   There is no delete keyword in java  Garbage collector is responsible for all deletes Java is free from dangling reference and garbage problem Why java does not support delete keyword? Java does not support delete keyword because there is a garbage collector in java This automatically runs and collects dangling or un-referenced objects.That is why there is no delete keyword in java like C++. Garbage collector can be explicitly like that System.gc();  When a life of object ends garbage collector runs automatically Example 1 { int a=5;// life Time start here a++; } //life time expire Example 2   Let Student is a class Student s;   // creating a reference of Student class    {Student t; //...