Create objects in Java
There are several ways by which we can create objects of a class in java as we all know a class provides the blueprint for objects, you create an object from a class
Methods:
- Using new keyword
- Using new instance -[TBD]
- Using clone() method -[TBD]
- Using deserialization -[TBD]
- Using newInstance() method of Constructor class-[TBD]
Method 1: Using new keyword
Using the new keyword in java is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of objects are created in this way. By using this method we can call any constructor we want to call (no argument or parameterized constructors).
Example:
class School {
String student_name = "Kithi"
public static void main(String[] args)
{
School obj = new School();
System.out.println(obj.name);
}
}
output:
Kithi
Method calling:
Example
Create a method named myMethod() in Main:
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
}
myMethod() prints a text (the action), when it is called. To call a method, write the method’s name followed by two parentheses () and a semicolon;
Example
Inside main, call myMethod():
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "Hello World!"
Static vs. Non-Static
You will often see Java programs that have either static or public attributes and methods.
In the example above, we created a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects:
Example
An example to demonstrate the differences between static and public methods:
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args) {
myStaticMethod();
Main myObj = new Main();
myObj.myPublicMethod();
}
}
Method definition:
Modifiers—such as public, private,default and protected .
The return type—the data type of the value returned by the method, or void if the method does not return a value.
The method name—the rules for field names apply to method names as well, but the convention is a little different.
The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.
Naming a Method
Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:
run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty
Example:
class Bank{
public static void main (String [] args)
{
Bank manager = new Bank ();
manager.giveLoan(10000); //method calling
}
public void giveLoan(int amount) //method signature
{
int i=amount; //method definition
System.out.println("loan amount is "+i);
}
}
Output
loan amount is 10000
References:
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://www.w3schools.com/java/java_class_methods.asp
https://www.geeksforgeeks.org/different-ways-create-objects-java/
Leave a comment