answersLogoWhite

0


Best Answer

for creating objects use the new operator along with a call to the constructor. for ex Triangle t = new Triangle(); In this statement the new operator creates a triangle object and the constructor is called which initializes the object and then new returns a reference of the object which is stored in the reference var "t" of type Triangle.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

To create an instance of an object, you just need to call its constructor. This, specified in the object's class file, will create the object. For example, to create a new object dog:

Dog woof = new Dog();

woof is the name of my new dog, Dog() is the constructor.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

We can create instances of a class by using the newkeyword.

class MyClass {

public static void main(String[] args) {

// Create a new Object object

Object obj = new Object();

// Create a new MyClass object

MyClass mc = new MyClass();

// Create a new String object

String str = new String();

// Create a new String object with some parameters

str = new String("This is a String");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Objects are constructed. You can't make a new object without invoking a constructor. In fact, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its superclasses including the Object class itself! Constructors are the code that runs whenever you use the keyword new.

Constructor Basics

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods). You can't always make that work for your classes; occasionally you have a class where it makes no sense to create an instance without supplying information to the constructor. A java.awt.Color object, for example, can't be created by calling a no-arg constructor, because that would be like saying to the JVM, "Make me a new Color object, and I really don't care what color it is...." Do you seriously want the JVM making your color choices?

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Objects are created when the JVM reaches a piece of code that has an invocation of the "new" keyword against some class name.

And, the "new" keyword is how objects get created in Java.

Ex:

Ferrari obj = new Ferrari();

The above line would create an object of the class Ferrari when executed. This line would in turn call the constructor of the Ferrari class which in turn creates and initializes an object of that class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

You can create an object in java using the new keyword. The new keyword invocation will call the constructor of the class and then create an object of the class.

Ex:

private ExampleTest obj = new ExampleTest();

here obj is the object that will get created when we use the new keyword on the class name

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

You would usually use the "new" operator:new MyClass();

If the constructor requires parameters, you pass them inside the parentheses. The object reference will usually be assigned to a variable, so you can use it later:

MyClass x; // Variable declaration

x = new MyClass(); // Instantiate an object, and assign the object reference to "x"



This answer is:
User Avatar

User Avatar

Wiki User

13y ago

the new operator is used to create objects in java

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

class Demo

{

public static void main(String[] args)

{

Demo d = new Demo();//the class is instantiated to create an object

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You cannot create an object in Java without using the newoperator.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you can create an object of a class in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


What is classes in java?

class is a blueprint which does not have its own existence but it can pass all of its feature to its child classes.


What is the Object class parent?

Object is the topmost class in the Java Class hierarchy. There is no Class above Object. All classes in Java are implicitly derived from Object.


What is a new keyword in java methods?

new is a keyword to create a instance of object any class.


How oops concept is implemented in java?

OOP stands for Object Oriented Programming. Everything in Java is an Object. Any class you create extends the Object class by default thereby making everything in Java an object. Moreover, you can use features like Inheritance, Polymorphism, Encapsulation etc which are OOP concepts thereby making Java an Object Oriented Programming Language


Is there class name as object in java?

Yes, the base class for all other Java classes is Object.


What is object class in java?

object class is a super class for all other class...


How is Java persistence done?

Java persistence is implemented using serialization. Serialization is a technique in java using which the contents of a java object (A class instance) can be written into a flat file. This value can be unserialized or deserialized at a later point of time to create the object. Any class that implements the Serializable interface can be serialized.


What is topleval class in java?

The top level class in Java is "Object." All other classes are subclasses of Object by default.


The top most class in java?

All classes in java must inherit from the Object class


Why object is the super class in java?

The answer to this is related to the idea of inheritance in general - the idea of inheritance is that you define a common set of behaviors, that apply to all subclasses. Anything defined in the "Object" class is available to all classes you create. Look in the documentation for the description of the "Object" class, to see what methods are available in all Java classes.


What is current instance in java?

Instance refers to one item of a particular type. for ex: you are one instance of a human, similarly I am one instance of human. An instance in object oriented terms refers to one item of a particular object type.