this Keyword in Java

All java classes contain a hidden data member named this. The this member is can be used at runtime to reference the object itself. Each object of a class contains a this pointer that points to the object for which the class member is currently executing. Lets see with a Example: // Welcome to CodeParent.com //Example program for this keyword public class Employee { static String Studnet_Name; static int Studnet_Age; public Employee(String name, int age) { this.Studnet_Name = name; this.Studnet_Age = age; } public static…

Continue reading »

Overloading Methods in Java

Java allows you to create two or more method with the same name but with different signatures. The signature includes the numbers, type, and the sequence of the arguments passes to a method. This capability to overload a method is referred to as overloading methods. Example1: // Welcome to CodeParent.com // Example for Overloading public class Addition { //Method name sum with 2 int parameters static int sum(int x, int y) { return (x + y); } //Methos name sum with 3 parameters like float,…

Continue reading »

Constructor in Java

Constructor is used to create new instance of a class. It is used to initialize all data members. Constructor is given the same name as class name. Each Java class can have one or more constructors. Even if a class doesn’t define a constructor java will provide a default constructor that takes no parameters. Example: class Student { String Name; String Dept; int Year; public Student(String n, String d, int y) { Name = n; Dept = d; Year = y; } } The above…

Continue reading »