Keywords
Reserved words with special meaning which is already defined in the Java language.
Keywords can't be used as name for variables, methods or classes. Keywords usually will be in lower case
E.g. public, return, final, static
final
final keyword serves different purpose depends upon the place it's been used
In methods: final methods cannot be overridden
In variables (Fields): final variables will be treated as constants
In class: final class can't be inherited further
E.g. String, all wrapper classes (Integer, Float) are final classes in Java
super
The super keyword in java is generally used to refer immediate parent class object
super is used to refer immediate parent class variable & methods
super() is used to invoke immediate parent class constructor
Whenever we call the overridden method, we can only call the sub class implementation of the method. If we want to call super class implementation, use super keyword
package
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer
Default imported package in all Java class is java.lang.*
To use a package, we use the keyword import.
static
static keyword serves different functionality depends on the place it's been used
In methods: When a method is declared as static, it can be accesses using the class name even without creating an object for the class. These methods are called as class methods.
In variables (Fields): When number of objects are created from the same class, they each have their own distinct copies all variables. If we have to create variables that are common to all objects, we should declare them as static. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object
In class: Only nested class (A class declared inside another class) can be declared as static in Java. Normal classes can't be declared as static
Just to recall, to access a method lies inside a Class, we need to create Object for that class as the first step.
But What if, We don't want to create an object for a class ?
Is there a way to access the methods without Object ?
Yes, we can, by declaring the method as static
static methods can be accessed using the Class Name itself
. We don't need to create Object
public class VoidMethodSample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Running a School
System.out.println("Please Enter Student Name");
String name = scanner.next();
System.out.println("Please Enter Student age");
int age = scanner.nextInt();
System.out.println("Please Enter your ZipCode");
int pinCode = scanner.nextInt();
VoidMethodSample.validateStudentInput(name, age, pinCode);
}
static void validateStudentInput(String name, int age, int pinCode) {
if (name.length() <= 10 && age > 7 && pinCode >= 100000 && pinCode <=999999) {
System.out.println("Thank you for your input");
} else {
System.out.println("Something's wrong");
}
}
}
Focus on the below lines taken from above program
VoidMethodSample.validateStudentInput(name, age, pinCode);
Since the validateStudentInput
method is static, we used the class name VoidMethodSample
to access the validateStudentInput
method
- REMEMBER: Methods that are declared as static can be accessed with ClassName as well by creating Object
class StudentInfo {
static void validateName(String name) {
}
}
// Accessing using ClassName
StudentInfo.validateName("Vicky");
// Accessing using Object
StudentInfo obj = new StudentInfo();
obj.validateName("Vicky");
instanceof
instanceof operator is used to check whether the object is an instance of the specified type. It will return either true or false.
class Student{
}
class Hostel_Student extends Student{
}
class Library_Member extends Student{
}
Student s1 = new Student();
Student s2 = new Hostel_Student();
Student s3 = new Library_Member();
if(s1 instanceof Student) // Result is true
if(s2 instance of Hostel_Student) //Result is true