OOPS Studio
OOPS Studio

Reputation: 835

What's the Java equivalent of defining a classless object in JavaScript?

I'm wondering if I can declare an object in-line in Java like I can in JavaScript. For example, in JavaScript I could say

let nameOfObject = {
    anyKey: "my value",
    someOtherValue: 5
};

console.log(nameOfObject.anyKey);// This logs "my value"

and this will create a classless object and assign nameOfObject as its "pointer" of sorts... How can I do this in Java? How do I create an object without a class?

I found one possible solution which looks like

Object nameOfObject = new Object(){
    public String anyKey = "my value";
    public int someOtherValue = 5;
};

System.out.println(nameOfObject.anyKey);

But that doesn't seem to compile, and says "anyKey cannot be resolved" and according to some other sources, will not work at all...

So I might be doing something very wrong, or maybe there's a different method, or maybe it's just straight up not possible with Java and I need to do it a different way...


class Some{
    public String anyKey;
}

Some some = new Some() {
    this.anyKey = "my value";
};

This code throws an error saying Syntax error, insert ";" to complete declaration, however

class Some{
    public void anyMethod() {
        
    }
}
        
Some some = new Some() {
    public void anyMethod() {
        System.out.println("Okay");
    }
};

Works perfectly fine?

I don't understand what's going on here, but I would really appreciate an explanation and/or a solution (if one exists).

Upvotes: 5

Views: 1778

Answers (3)

Mick
Mick

Reputation: 796

You can't really think of it as an "object" in Java. JavaScript objects are unique implementations of HashTables. If you want similar functionality, you can use a Java HashMap or ConcurrentHashMap(HashTable), depending on your specific requirements.

// we only add String->Double, but since you want to add "any type",
// we declare Map<Object, Object>
HashMap<Object, Object> map= new HashMap<>(); 
map.put("Zara", new Double(3434.34));
map.put("Mahnaz", new Double(123.22));

String[] names = map.keys();
      
while(names.hasMoreElements()) {
    str = (String) names.nextElement();
    System.out.println(str + ": " + map.get(str));
}

Upvotes: 4

Daniel
Daniel

Reputation: 113

JavaScript classes are different from Java classes, JavaScript is not a class-based object-oriented language, an object in JS is used as a template to hold properties, but in Java classes could hold variables (properties) and functions, so to initialize an object in Java there has to be a class for that object, you can think of it as a blueprint of how the object will be initiated for example to create a class:

public class Example {
    public String link = "https://stackoverflow.com/";

    public void printLink() {
        System.out.println(link); // Prints the string stored in link
    }

    public void openLink() {
        // Some code to open the link in the browser
    }
}

To initialize an object from the class you created call this in your main or somewhere where the code will run:

Example example = new Example();
example.printLink(); // Printed the link
example.openLink(); // Opened the link
example.link = "https://example-link.com/"; // Changed the link

Note that the Example class could be in a different file and the file name has to be the same as the class name, here it is (Example.java)

Anyway if you are looking for an equivelant for JavaScript classes HashMap or HashTable could be very close.

Upvotes: 1

EKW
EKW

Reputation: 2113

So, all objects in java have a class associated with them. Even raw Objects have a class: The Object class! You won't be able to create an object without a class because all values need to have a type of some sort. But that's really not a huge problem. If you need an object that stores certain values, you can just create a class that has space for those values. There's really no situation where you would not be able to do this.

That being said, if you just want a way of mapping keys to values, I'd recommend looking into the Map data structure.

Upvotes: 1

Related Questions