Accessing Class
In Java, class members include fields (variables) and methods (functions).
Accessing them depends on:
1. Whether they are static or non-static
2. Their access modifiers (public, private, protected, default)
3. Whether you are accessing them inside or outside the class
1. Accessing Members Within the Same Class
public class MyClass {
int a = 10; // instance variable
static int b = 20; // static variable
void display() {
System.out.println(a); // access non-static field directly
System.out.println(b); // access static field directly
public static void main(String[] args) {
MyClass obj = new MyClass(); // create object
obj.display(); // call non-static method
2. Accessing Members From Another Class (Same Package)
public class A {
public int x = 5; // public
int y = 10; // default
private int z = 15; // private
public class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.x); // accessible (public)
System.out.println(obj.y); // accessible (default, same
package)
// System.out.println(obj.z); // not accessible (private)
3. Accessing Static Members Without an Object
class Demo {
static int count = 100;
static void show() {
System.out.println("Count: " + count);
public class Main {
public static void main(String[] args) {
Demo.show(); // static method accessed without
object
System.out.println(Demo.count);
4. Accessing Non-Static Members Requires an Object
class Demo {
int number = 50;
void print() {
System.out.println("Number: " + number);
}
public class Main {
public static void main(String[] args) {
Demo obj = new Demo(); // create object
obj.print(); // call non-static method
System.out.println(obj.number); // access non-static field
Access Modifiers Summary
Modifier Same Class Same Package Subclass Other Packages
public
protected (except subclass)
(default)
private
Constructors: -
Constructors play an important role in object creation. A constructor is a special block
of code that is called when an object is created. Its main job is to initialize the object, to
set up its internal state, or to assign default values to its attributes. This process happens
automatically when we use the "new" keyword to create an object.
Characteristics of Constructors:
• Same Name as the Class: A constructor has the same name as the class in
which it is defined.
• No Return Type: Constructors do not have any return type, not even void.
The main purpose of a constructor is to initialize the object, not to return a
value.
• Automatically Called on Object Creation: When an object of a class is
created, the constructor is called automatically to initialize the object’s
attributes.
• Used to Set Initial Values for Object Attributes: Constructors are
primarily used to set the initial state or values of an object’s attributes when
it is created.
Example: -
class A{
int a; String name; boolean c;
A(){
a=100; name="Ankit"; c=true;
void Show(){
System.out.println(a+" "+name+" "+c);
public class Const {
public static void main(String[] args) {
A ak = new A();
ak.Show();
Types of Constructors in Java
A constructor that has no parameters is known as default constructor. A default
constructor is invisible. And if we write a constructor with no arguments, the compiler
does not create a default constructor. Once you define a constructor (with or without
parameters), the compiler no longer provides the default constructor. Defining a
parameterized constructor does not automatically create a no-argument constructor, we
must explicitly define if needed. The default constructor can be implicit or explicit.
• Implicit Default Constructor: If no constructor is defined in a class, the
Java compiler automatically provides a default constructor. This constructor
doesn’t take any parameters and initializes the object with default values,
such as 0 for numbers, null for objects.
• Explicit Default Constructor: If we define a constructor that takes no
parameters, it's called an explicit default constructor. This constructor
replaces the one the compiler would normally create automatically. Once
you define any constructor (with or without parameters), the compiler no
longer provides the default constructor for you.
Example: This program demonstrates the use of a default constructor, which is
automatically called when an object is created.
class A {
A() {
System.out.println("Default constructor called");
public static void main(String[] args) {
A obj = new A();
2. Parameterized Constructor
A constructor that has parameters is known as parameterized constructor. If we
want to initialize fields of the class with our own values, then use a parameterized
constructor.
Example: This program demonstrates the use of a parameterized constructor to
initialize an object's attributes with specific values.
class Student {
String name;
int age;
Student(String n, int a) { // Parameterized constructor
name = n;
age = a;
}
void display() {
System.out.println(name + " - " + age);
public static void main(String[] args) {
Student s1 = new Student("Ankit", 22);
s1.display();
3. Copy Constructor
Unlike other constructors copy constructor is passed with another object which copies
the data available from the passed object to the newly created object.
Note: Java does not provide a built-in copy constructor like C++. We can create our
own by writing a constructor that takes an object of the same class as a parameter and
copies its fields.
Example: This example, demonstrates how a copy constructor can be used to create a
new object by copying the values of another object's attributes.
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
// Copy constructor
Student(Student s) {
name = s.name;
age = s.age;
void display() {
System.out.println(name + " - " + age);
public static void main(String[] args) {
Student s1 = new Student("Ankit", 22);
Student s2 = new Student(s1); // Using copy constructor
s2.display();
Important Notes:
• Constructors can be overloaded (like methods) with different parameter
lists.
• Constructors cannot be static, abstract, or final.
• If you define any constructor, Java does not provide the default
constructor automatically.