KEMBAR78
Variable and Methods in Java | PPTX
Variables and Methods Tips and Tricks
Jagriti Srivastava
Tips and Tricks
 Follow the naming Convention Rule
 Meaning full name
 Different variable different name in same scope
 Variables based on scopes
 No keyword no reserve word(remember the color)
 Don’t start with digit
 Single name is variable
 Verb or action is method followed by ()
 Small case with underscore and camel case are methods and variables
 Word with Capital letter at start is Class name
 All small letter in word is package
Tips and Tricks
 String firstName → variable to hold first name
 void saveName() → method
 Person → Class
Variable
 Class or instance variable
 Inside class with the scope within class
 Static variable
 Whose value doesn’t change
 Local variable
 Inside method with the scope within method
Methods
Static Method Non Static Method
Method with
Return type and
Argument
Method with no
Return type and
No Argument
Method with no
Return type and
Argument
Method with
Return type and
No Argument
Code Example
public class TestOne {
int n2 = 20; //class variable or instance variable
static int n3 = 30; //static variable
static char c1 ;
String str2;
public static void main(String[] args) {
int n1 = 10; //local variable
System.out.println("the value of a is : "+n1);
//System.out.println(n2);cant be called here because non static field cant be called inside static method
System.out.println(n3);
showValue(); //since this is static method
TestOne tOne = new TestOne();
tOne.display();
c1 = 'J';
System.out.println(c1);
}
public static void showValue(){//static method with no return type and no argument
System.out.println(n3);
//System.out.println(n1);cant be called here because local variable at main method
}
public void display(){//non static method with no return type and no argument
System.out.println(n2);
System.out.println(n3);
String str1 = "hello world";
System.out.println(str1);
str2 = "Hello test";
}
}
Output
 the value of a is : 10
 30
 30
 20
 30
 hello world
 J

Variable and Methods in Java

  • 1.
    Variables and MethodsTips and Tricks Jagriti Srivastava
  • 2.
    Tips and Tricks Follow the naming Convention Rule  Meaning full name  Different variable different name in same scope  Variables based on scopes  No keyword no reserve word(remember the color)  Don’t start with digit  Single name is variable  Verb or action is method followed by ()  Small case with underscore and camel case are methods and variables  Word with Capital letter at start is Class name  All small letter in word is package
  • 3.
    Tips and Tricks String firstName → variable to hold first name  void saveName() → method  Person → Class
  • 4.
    Variable  Class orinstance variable  Inside class with the scope within class  Static variable  Whose value doesn’t change  Local variable  Inside method with the scope within method
  • 5.
    Methods Static Method NonStatic Method Method with Return type and Argument Method with no Return type and No Argument Method with no Return type and Argument Method with Return type and No Argument
  • 6.
    Code Example public classTestOne { int n2 = 20; //class variable or instance variable static int n3 = 30; //static variable static char c1 ; String str2; public static void main(String[] args) { int n1 = 10; //local variable System.out.println("the value of a is : "+n1); //System.out.println(n2);cant be called here because non static field cant be called inside static method System.out.println(n3); showValue(); //since this is static method TestOne tOne = new TestOne(); tOne.display(); c1 = 'J'; System.out.println(c1); } public static void showValue(){//static method with no return type and no argument System.out.println(n3); //System.out.println(n1);cant be called here because local variable at main method } public void display(){//non static method with no return type and no argument System.out.println(n2); System.out.println(n3); String str1 = "hello world"; System.out.println(str1); str2 = "Hello test"; } }
  • 7.
    Output  the valueof a is : 10  30  30  20  30  hello world  J