Powered by Blogger.
Showing posts with label Constructors. Show all posts
Showing posts with label Constructors. 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();
  }
 }

Java Programming | Declaring Classes, Attributes, Constructors, and Methods

Declaring Classes, Attributes, Constructors, and Methods:

A class is a software blueprint. A class defines the set of data elements (called attributes), as well as the set of behaviors or functions (called methods). Together, attributes and methods are called members of a class. In Java technology, class name usually starts with a capital letter, attribute and method name usually starts with a small letter. The access modifier for the classes in the Java technology can be public or default. The Java technology class, attribute, constructor, and method declaration.

A constructor is a set of instructions designed to initialize an instance or object. The name of the constructor must always be the same as the class name. Constructor do not return any value. Valid modifiers for constructors are public, protected, and private.

The Default Constructor: Every Java class has at least one constructor. If you do not write a constructor in a class then Java technology provides a default constructor for you. This constructor takes no arguments and has an empty body.

Example: 

 public class MyDate{
 private int day;
 private int month;
 private int year;
 public MyDate(){
 day=1;
 month=6;
 year=1979;
 }
 public void showDate(){
 System.out.println(”Day:”+day);
 System.out.println(”Month:”+month);
 System.out.println(”Year:”+year);
 }
 public static void main(String[ ]args){
 MyDate date = new MyDate();
 date.showDate();
 }
 }