Access Modifier ✔
Access Modifier
== Everywhere ===========
|
==Same Package======== |
| |
== Sub Class ===== | |
| | |
==Parent Class== | | |
| | | | |
| PRIVATE | | | |
| | | | |
================ | | |
PROTECTED | | |
=================== | |
DEFAULT | |
====================== |
PUBLIC |
=========================
static
- variable is declare outside method, constructor or any block
- variable can be used as common property(variable) of all objects of it's class as it's belong to class not object
- don't make multiple copy for multiple object.
- method can't use
thisorsuperkeyword. - method can't use non-static member
- don't need to use class or object name if both are in the same class
Belongs to the Class: doen't make multiple instances for multiple object.
class Parent{
static int x=5;
void show(){
System.out.println(x);
}
}
class Test{
public static void main(String[] args){
Parent p1= new Parent();
Parent p2= new Parent();
p1.x=10;
p2.x=10;
p1.show();
p2.show();
}
}
As x belongs to the Parent class, any modification to x through one instance will affect all instances of the class.
No Access to Instance Variables or Methods: Static methods can only access other static methods or static variables of the class. They cannot directly access instance variables or methods because they are not associated with an object (instance) and thus have no knowledge of instance-level data. So, we have to create an instance of the class inside static method to access non-static property.
class Test{
static int x=5;
int y=10;
public static void main(String[] args){
System.out.println(x);
// System.out.println(y); // on-static variable y cannot be referenced from a static context
Test t=new Test();
System.out.println(t.y);
}
}
private
- accessible within the class it is declared
- can't accessible not even in child class
danger
- can't use in class/interface, it can cause compilation error
class Bank {
private class Account {
int balance = 1000;
}
void showBalance() {
Account a = new Account();
System.out.println(a.balance);
}
}
- Top-level classes cannot be private
- Only inner classes can be private :::
protected
- accessible anywhere within the same package, no need to be immediate child, any child
- can't use in class/interface, it can cause compilation error
default
- accessible anywhere within the same package
Package
A package is a way to group related classes and interfaces together in a folder structure. Packages help organize code, avoid naming conflicts, and control access to classes. Java has two types of packages:
- Built-in Packages: Java provides several standard packages, like
java.lang,java.util, andjava.io, which contain commonly used classes and interfaces. - User-defined Packages: Developers can create their own packages to group related classes as needed.
Declare Package
To declare a package, you use the package keyword at the beginning of a Java file:
package com.example.myapp;
After declaring a package, the file becomes part of that package, and all classes inside it are associated with it.
- there must be not more than one public class per file
- file name must be matched with the public class
Example:
// File: com/library/Book.java
package com.library;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
}
}
To resolve name conflicts in package, use fully qualified name
packageName.ClassName = new packageName.ClassName();
Package hierarchy is created and accessed through (.) dot operator.
Keywords
final
- class can't be inherited
- method can't be overriden
- variable can be inherited but can't modifyable
- final instance variable can only be initialized through constructor
this, super keyword
- anywhere in the program
this
- differentiate the local scope and class scope.
Passing Current Object as Argument:
class ClassB{
void show(ClassA a){
System.out.println(a.x);
}
}
class ClassA{
int x = 5;
ClassB b = new ClassB();
void show(){
b.show(this);
}
}
class Test{
public static void main(String[] args){
ClassA a = new ClassA();
a.show();
}
}
Returning the Current Object:
class ClassA{
int x = 5;
ClassA show(){
return this;
}
}
class Test{
public static void main(String[] args){
ClassA a = new ClassA();
ClassA s = a.show();
System.out.println(s.x);
}
}
super
- refer immediate parent class
this(), super() method
- only inside constructor
- must be first statement in a constructor.
- can't be use together
this()
super()
- invoke immediate parent class constructor