0% found this document useful (0 votes)
19 views7 pages

Main Method Lyst3867

Uploaded by

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

Main Method Lyst3867

Uploaded by

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

Main Method

Object creation in java

How to create an object in java?


A class provides the blueprints for objects. So basically, an object is created from a class.

In Java, the "new" keyword is used to create new objects. There are three steps when creating an object
from a class −

● Declaration − A variable declaration with a variable name with an object type.


● Instantiation − The 'new' keyword is used to create the object.
● Initialization − The 'new' keyword is followed by a call to a constructor.
● This call initializes the new object.

Syntax: class_name object_name = new class_name();

For example: Car c1 = new Car();

Even there are 4 functions or method present in this program will not get executed because it requires
the control of execution to begin.

The control of execution is nothing but "main method / function" in C language.

1
2
Main method in Java
C Language

#include <stdio.h>
void main()
{
printf("HELLO WORLD");
}

Main Method in JAVA

Step 1

In Java, basics rule of java is that every single method / function compulsory present within a Class.

Will the above program execute?

3
Control cannot see the main method because we have enclosed by class Demo.

To make the main method visible to the control though it is enclosed within class.

How can we make it Visible?

By attaching PUBLIC

Step 2

Will the above program execute?

Control can see the main method because we have declared it has public unfortunately it is not
accessible by the control.

To make the main method visible to the control though it is enclosed within class.

How can we make it Accessible?

By attaching STATIC

Step 3

4
Will the above program execute now?

Unfortunately, it will not show the output because JVM will start looking for Identifier as a starting
point.

What is this Identifier?

It is nothing but String [] args

It is the command line argument which stores data and it is nothing but an array.

Step 4

String [] args
What is args?

5
Arguments means data passed on the command line.

Args is technically called as "Dynamic Array" using which the command line arguments are collected.

In java, along with the control of execution we can also give inputs/ data to the main method.

To collect the input / data there is args. Initially args is an empty basket.

Different way to declare Syntax / Signatures


● public static void main (String [] args)
● static public void main (String [] args)
● public static void main (String args [])
● public static void main (String...args)

6
Key points
● Always save the source file same as the class name in which main() is present along with the
.java extension if it is written in notepad.
● When javac source_file_name.java is run in command prompt then a .class file creates which is
in byte code format.
● To convert the byte code file to machine level all you have to do is run the following line in
command prompt: java source_file_name
● If the main method is not made as public then during execution an error is popped saying main
method not found.
● If the main method is not static then during execution it shows error as main method not static.

You might also like