Wondering 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();

Main method in java
 Every java program must have a method, only then it will start the execution. That method is called as main(). A Java application is a public Java class with a main() method.
➥The main() method is the entry point into the application.
➥The signature of the method is always: 
public static void main(String[] args)
➥Command-line arguments are passed through the args parameter, which is an array of strings.
➥In C language also irrespective of how many functions are there inside the program, without the control of execution program cannot execute.
Control of execution is given to a program by creating a special method called as main().
➥In java, no functions/methods are directly accessible. All methods will be present inside the class.
➥Similar to C even in java control of execution is given to a special method called as main().

Now let’s print

As in C, we have printf(); to print anything on the monitor, in java we have a similar pipe which is System.out.println(); 
To make the main method visible to class you have to attach a word called as public to the main(). And to access the method without object creation another word must be attached after "public" that is "static".
Syntax: public static void main()
After giving the access the last step is to pass String[] args as parameter to the main().
Syntax: public static void main(String[] args)

class Abc
{
          public static void main(string[] args)
          {
            System.out.println("Hello World!");
           }
}                               

Points to remember while executing

➤Always save the source file the 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 .class file creates which is in bytecode format.
➤To convert the bytecode file to machine level all you have to do is run the following line in the 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. Output Passing data via command promptTo collect the inputs given from the user in java we have a basket called as args.

Comments

Popular posts from this blog

Comments

Introduction to microprocessor

Accessing the value of a variable