Posts

Showing posts from January, 2021

The this keyword in java

Image
The this keyword in java The this keyword refer to current class object in java . this keyword can be used for following  this() can be used to invoke current class constructor this can be used to invoke current class method this can be used to return current class object this can be used to refer current class instance variable  this can be used to pass an argument in method call this can be used to pass an argument in constructor call 1) this: to refer current class instance variable class Student{   int rollno;   String name;   float fee;   Student(int rollno,String name,float fee) {   this.rollno=rollno;   this.name=name;   this.fee=fee;   }  void display() { System.out.println(rollno+" "+name+" "+fee);  } public static void main(String args[]){   Student s1=new Student(1,"anil",50f);   Student s2=new Student(2,"sunil",60f) ;     s1.display(); ...

What is Static Keyword in java?

Image
  Static Keyword in java Static keyword is basically used for the memory management purpose in java. Static keyword belongs to class and  can be apply with the following 1. Variable 2. Method 3. Block 4. Nested class 1. Java Static Variable: If you declare any variable with static keyword then it is called static Variable The static variable gets memory once in the class area at the time of class loading we can create static variable at class level.  static keyword can be used with class name without creating instance Example of java static variable public class StaticKeywordDemo { static int a=10; public static void main(String h[]) { System.out.println(StaticKeywordDemo.a); //className.variable-name } } 2. Java Static Method:  If you declare any method with static keyword then it is called static method static keyword can be used with class name without creating instance static method belong to class rather then object of class public class StaticK...

Method in java

Image
    METHOD IN JAVA Method in java is a  block of code that are used to perform a specific task. It is used for code re-usability It executes only when invoked For making modification easier How to declare method : Syntax:  types of method in java:  There are two type of method in java 1.User defined method 2.System define method 1.User defined method:  The method which is defined by user is called user defined method  in java Example 1 : Without return type& without parameter Class MethodDemo{ public void Demo() //method is declare { System.out.println("hello "); } public static void main(String args[]) { MethodDemo demo=new MethodDemo(); demo.Demo(); //Demo() method is invoked or called } } output: hello Example 1 : Without return type & with parameter Class MethodDemo{ public void Add(int a, int b) //method is declare { System.out.println(a+b); } public static void main(String args[]) { MethodDemo demo=new MethodDemo(); demo.Add(10,20);...

Constructor in java

Image
 Constructor in java Constructors are used to initialize the object’s state.It is called when the instance of class is created. It is special type of method which is used to intialize the object.Every time when  object is created using new() keyword at least one  constructor is called and memory is allocated to the object of the class. If there are no constructor then  java  compiler provide default constructor. Class Name and constructor name must be same. Constructor does not have any return type. Java constructor can not be   abstract, static, final, and synchronized. We can use access modifiers with constructor that control object creation. Types of constructor There are two types of constructor 1.Default Constructor 2.Parameterized Constructor 1.Default Constructor: A constructor that does not have any parameter is called default constructor. Syntax: Class_Name(){} Example of default constructor in java:  Class Demo { Demo() //default const...