Powered by Blogger.
Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Write a Java Program using This Reference - About Java

Java Program using This Reference:

In Java technology, this keyword is used to resolved ambiguity between instance variables and parameters. It is also used to pass the current object as a parameter to another method.

This Reference Program:

 public class AddNumbers{
 private int num1,num2,result;

 public AddNumbers(int num1,int num2){
 this.num1 = num1;
 this.num2 = num2;
 }

 public void add(){
 result=num1+num2;
 System.out.println(”Result is:”+result);
 }

 public static void main(String[]args){
 AddNumbers addnum = new AddNumbers(10,20);
 addnum.add();
  }
 }

Java~~Write a java program using Blocks~~


A block as a group of statements that are collected together called a compound statement. It is bound by opening and closing braces { }. A class definition is contained in a block. A block statement can be nested withing another block. In Java source code, block statements are executed first, then constructor statements are executed, and then method statements are executed.

Best Example: 

 public class BlockTest{
 public String info;

 public BlockTest(){
 info = ”Constructor:Executed in 2nd step”;
 System.out.println(info);
 }

 {
 info = ”Block:Executed in 1st step”;
 System.out.println(info);
 }

 public void show(){
 info = ”Method:Executed in 3rd step”;
 System.out.println(info);
 }

 public static void main(String[]args){
 BlockTest bt = new BlockTest();
 bt.show();
  }
 }