0% found this document useful (0 votes)
4 views10 pages

Java Programming Constructors

The document provides an overview of Java programming concepts, including the enhanced for-each loop, types of methods, access specifiers, and constructors. It explains the purpose and limitations of the for-each loop, differentiates between predefined and user-defined methods, and outlines the rules and types of constructors. Additionally, it highlights the differences between constructors and methods, along with examples of default and parameterized constructors.

Uploaded by

iq06845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Java Programming Constructors

The document provides an overview of Java programming concepts, including the enhanced for-each loop, types of methods, access specifiers, and constructors. It explains the purpose and limitations of the for-each loop, differentiates between predefined and user-defined methods, and outlines the rules and types of constructors. Additionally, it highlights the differences between constructors and methods, along with examples of default and parameterized constructors.

Uploaded by

iq06845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Programming

Er. Mohit Paul


Java For Each Loop
Java For-Each Loop (Enhanced For Loop)
•Introduced in: J2SE 5.0
•Purpose: Simplifies traversal of arrays and collections.
•Advantages:
•Easier and more readable than traditional for loop.
•Reduces chances of bugs (no index handling).
•Limitations:
•Cannot traverse in reverse order.
•Cannot skip elements or access specific indexes.
•Cannot traverse only odd/even elements.
•Recommendation:
•Best used for simple element-by-element traversal of arrays and collections.

for(data_type variable : array |


collection)
{
//body of for-each loop
}
class Main
{
public static void main(String args[])
{
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for-each
loop
for(int i:arr){
[Link](i);
}
}
}
Java Methods

Types of Methods
There are two types of methods in Java:
•Predefined Method
•User-defined Method
Access Specifier

 Access Specifier: Access specifier or modifier is the access type of the


method. It specifies the visibility of the method. Java provides four types
of access specifiers:
• public: The method is accessible to all classes when we use the public
specifier in our application.
• private: When we use a private access specifier, the method is
accessible only in the classes in which it is defined.
• protected: When we use a protected access specifier, the method is
accessible within the same package or subclasses in a different
package.
• default: When we do not use any access specifier in the method
declaration, Java uses the default access specifier by default. It is visible
only from the same package.
Java Constructors
Definition:
A constructor is a special block of code (like a method) that is called automatically when an object is
created using the `new` keyword.
It is used to initialize objects.

Object Creation:

* Memory for the object is allocated when the constructor is called.


* If no constructor is defined, the **Java compiler provides a default constructor automatically.

Why Called Constructor:

* It “constructs” values (initializes data) at the time of object creation.

Need of Constructor:

* Not mandatory to define one manually.


* Java creates a default constructor if none exists.
Rules for Creating a Constructor

1. Constructor name must match the class name.


2. It has no explicit return type (not even `void`).
3. It cannot be abstract, static, final, or synchronized.
4. Access modifiers (`public`, `private`, `protected`, or default) can be
used to
control object creation.

Types of Constructors

1. Default Constructor (No-arg Constructor):

* Takes no arguments.
* Automatically provided if no constructor is defined.

2. Parameterized Constructor:

* Takes parameters to initialize objects with specific values.


Difference between Constructor and
Method
Java Constructor Java Method

A constructor is used to initialize A method is used to expose the


the state of an object. behavior of an object.

A constructor must not have a A method must have a return type.


return type.

The constructor is invoked The method is invoked explicitly.


implicitly.
The Java compiler provides a The method is not provided by the
default constructor if we do not compiler in any case.
have any constructor in a class.

The constructor's name must be The method name may or may not
same as the class name. be same as the class name.
Java Default Constructor
//Java Program to create and call a default
constructor
class Bca
{
//creating a default constructor
Bca()
{
[Link](“ Bca Constructor created");
}
}
public class Main
{
//main method
public static void main(String args[])
{
//calling a default constructor
Bca b=new Bca();
}
}
Parameterized Constructor
class Student
{ //Main class to create objects and class
int id; methods
String name;
//creating a parameterized constructor public class Main
Student(int i,String n) {
{ public static void main(String args[])
id = i; {
name = n; //creating objects and passing values
} Student s1 = new Student(1,“Rohan");
//method to display the values
void display() //calling method to display the values of
{ object
[Link](id+" "+name); [Link]();
} }
} }

You might also like