A. Local Variables
Syntax :
class {
constructor {
// Use Local Variables within Constructor
}
method {
// Use Local Variables within Method
block {
// Use Local Variables within Block
}
}
}
VarType_Local.java
package com.variables;
public class VarType_Local {
VarType_Local(){
int a = 1000;
System.out.println("Constructor - Local Variable 'a' : " + a);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 5;
int b = 10;
int c;
c = a + b;
System.out.println("Method - Local Variable 'a' : " + a);
System.out.println("Method - Local Variable 'b' : " + b);
System.out.println("Method - The Addition of Local Variables : " + c);
for(int i=1; i <=5; i++) {
System.out.println("Block - Local Variable : " + i);
}
}
}
Sample Output
Constructor - Local Variable 'a' : 1000
Method - Local Variable 'a' : 5
Method - Local Variable 'b' : 10
Method - The Addition of Local Variables : 15
Block - Local Variable : 1
Block - Local Variable : 2
Block - Local Variable : 3
Block - Local Variable : 4
Block - Local Variable : 5
B. Instance Variables
Syntax :
BaseClass {
// Declare Instance Variables Here..
constructor {
// Use Instance Variables
}
function {
// Use Instance Variables
block {
// Use Instance Variables
}
}
}
SubClass {
// Use Instance Variables (when instance variables are given accessibility)
}
VarType_Instance.java
public class VarType_Instance {
public int id;
protected String name;
private String dept;
VarType_Instance(int x, String y, String z) {
id = x;
name = y;
dept = z;
}
void display() {
System.out.println("Id : " + id);
System.out.println("Name : " + name);
System.out.println("Department : " + dept);
}
}
class MainClass {
public static void main(String args[]) {
VarType_Instance obj = new VarType_Instance(11, "Priyanka", "IT");
obj.display();
}
}
Sample Output
Id : 11
Name : Priyanka
Department : IT
C. Class (or) Static Variables
Syntax :
BaseClass {
// Declare Class/Static Variables Here..
constructor {
// Use Class/Static Variables
}
function {
// Use Class/Static Variables
block {
// Use Class/Static Variables
}
}
}
SubClass {
// Use Class/Static Variables (when Class/Static variables are given accessibility)
}
VarType_Static.java
public class VarType_Static {
public static final int id = 111;
public static final String name = "John";
protected static int salary = 50000;
void display() {
System.out.println("Id : " + id);
System.out.println("Name : " + name);
System.out.println("Salary : " + salary);
}
}
class MainClass {
public static void main(String args[]) {
VarType_Static obj = new VarType_Static();
obj.display();
}
}
Sample Output
Id : 111
Name : John
Salary : 50000
|
know more about WebDriver, TestNG, Jenkins, Framework, Page Object Model, Cucumber, Selenium Grid
Variable Types
Subscribe to:
Posts (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.