2

I was asked this question in an interview and I could not find any direct answer to it online.

"Can you create an object in Java without using a class?"

There is no context or addition to this question, so please do not label this question as incomplete or pointless. If this question is a trick one or needs context, let me know.

2
  • 3
    Technically, I would assume the answer is "no" because no matter what sort of object you create, it will implicitly extend the Object class. But perhaps that's not what you were being asked. For example, you can create an anonymous class instance from an interface. There's just not really enough information here to give an answer (which is how I would respond to that question if asked) Commented Jul 15, 2020 at 4:59
  • Can anyone answer the same question about python: "Can you create an object without using a class in Python?" It was a continuation of the same question in an interview with no other context. Commented Aug 27, 2020 at 13:26

2 Answers 2

6

Yes: arrays are objects, but there are no array classes.

The Java language and JVM specifications make a distinction between class instances and arrays in many places. Most notably:

JLS 4.3.1:

An object is a class instance or an array.

JLS 4.3.2:

All class and array types inherit (§8.4.8) the methods of class Object

JVM 3.9

Java Virtual Machine arrays are also objects.

(After a section that was dedicated to objects that are class instances)

Sign up to request clarification or add additional context in comments.

4 Comments

I wouldn't have thought of that. But if you give that answer in a job interview you'd probably be able to quote from JLS to prove it, because probably the interviewer wouldn' have heard of that either 😊
Okay, maybe there are distinctions made often. But Array class exists in Java, doesn't it? Which is the one instantiated, part of Java Collections Framework?
There is a class called Array in the reflection API and a class called Arrays in java.util, but they do not appear in the type hierarchy of array types. There is no int[] class.
@RoopaliNeeraj There is java.util.Arrays, a utility class for operation on arrays of <whatever> if that's what you mean - but as a class standalone, Array is not an instantiable class
2

Well, the answer of Joni is a very good one. I did not immediately think of that.

However, there is no way for you to instantiate an object without being in the context if a class.

A soon as you have written something like new int[0], you have already written class MyClass { or something like that. The new keyword is never valid outside the definition of a class.

So the actual answer is no.


So is this a trick question? Yes, absolutely. Is this a clear question? Not really. The answer may as well depend on what exactly is meant by the word "using". I would ask for clarity first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.