Class is the collection of object and properties, Properties in the sense it contain the methods, constructor and variables.For example the college contain the class rooms and the room is occupied by full of benches and chairs.
Syntax:
public class ja{
public static void main(String args[])
{
}
}
In the first line the public is the access specifiers and in java everything is a class and object . So we need to mention the class and ja is the class name of your project.
Then in the next line it contain the code that start with public it is also an access specifier , static is the keyword does not return anything , which means its not possible to return anything inside the void method.Main is the main method of the program. Inside the parenthesis you can able to see the string, In java string is an object and first letter of string should be capital letter . Then args[] is the command line argument in java and its used to get the input in the run-time.
Java is case sensitive so you need to write the letters more accurately. If it is capital letter then you should give the capital letter.
There are another type of constructor called parametrized constructor which is used to pass the values to the constructor for example ,
Syntax:
A(int a)
{
system.out.println(Rajesh);
}
Now we are going to call the constructor from the main method ,
Syntax:
public class A{
A(int a)
{
System.out.println(ToNy);
System.out.println(rAjEsH);
}
{
public static void main(String args[])
{
A obj= new A(25);
{
}
}
Syntax:
public class ja{
public static void main(String args[])
{
}
}
In the first line the public is the access specifiers and in java everything is a class and object . So we need to mention the class and ja is the class name of your project.
Then in the next line it contain the code that start with public it is also an access specifier , static is the keyword does not return anything , which means its not possible to return anything inside the void method.Main is the main method of the program. Inside the parenthesis you can able to see the string, In java string is an object and first letter of string should be capital letter . Then args[] is the command line argument in java and its used to get the input in the run-time.
Java is case sensitive so you need to write the letters more accurately. If it is capital letter then you should give the capital letter.
CONSTRUCTOR:
Constructor does not need object while creatin object itself constructor automatically called CONSTRUCTOR. It does not need anything. Constructor must have the class name this are the various rules of constructor.
Syntax:
class name()
{
//statements
}
A()
{
System.out.println(TONY);
}
Syntax:
A(int a)
{
system.out.println(Rajesh);
}
Now we are going to call the constructor from the main method ,
Syntax:
public class A{
A(int a)
{
System.out.println(ToNy);
System.out.println(rAjEsH);
}
{
public static void main(String args[])
{
A obj= new A(25);
{
}
}
OBJECTS
Object is the real world entity and it is the instance of a class or the behavior of the class. For example in a class there are benches and the chairs, Those are called the objects. If we create the object means the entire class can be accessible with the help of that object. With the help of object it is possible to call the method, variable an the datatype inside the class.
Syntax:
public class To{
public static void main (String args[])
{
To obj = new To();
}
}
In the above code we have been created the object inside the main method (To obj) this is the reference to the object, it will create the memory for your object, (new To();) this is used to allocate the memory according to the properties and it is also know as the constructor. This all together called as the object in java. To call the variable , method using object place the following code in your project
Example :
public class To{
int a=10;
void method()
{
System.out.println(Hello world);
}
Public static void main (String args[])
{
To obj = new To();
obj.method();
System.out.println(obj.a);
}
}