What is the syntax for defining a nested data object in Java?

When working with Javascript, I have the ability to define static variables like this:

var Services = {
Pandora : {name: "ziggy", password: "stardust"},
AppleMusic : {name: "steve", password: "jobs"},
SoundCloud : {name: "bedroom", password: "studio"}
};

I can then easily access the data members using object notation.

console.log(Services.Pandora.name);
console.log(Services.AppleMusic.name);
console.log(Services.SoundCloud.password);

However, when trying to achieve the same in Java, I find it challenging. The solutions discussed, such as nested classes or ArrayLists, seem overly complex and verbose. Is there a simpler way to accomplish this in Java? Any insights would be appreciated!

Later....

After some experimentation, I opted to use an enum to declare static variables in Java:

public enum Service {

    Batanga     ("Batanga","Batanga","my_name","my_password",""),
    Calm        ("Calm Radio","Calm Radio","my_name","my_password",""),

    ;

    private final String svcName;
    private final String svcFullName;
    private final String login;
    private final String pass;
    private final String url;

    Service(String svcName, String svcFullName, String login, String pass, String url) {
        this.svcName = svcName;
        this.svcFullName = svcFullName;
        this.login = login;
        this.pass = pass;
        this.url = url;
    }

    public String getSvcName() {
        return svcName;
    }

    public String getSvcFullName() {
        return svcFullName;
    }

    public String getLogin() {
        return login;
    }

    public String getPass(){
        return pass;
    }

    public String getUrl() { return url; }
}

Thank you for your help!

Answer №1

One possible approach could involve creating a Service class and storing instances of it in a Map, using the service name as the key:

public class Service {
    private String name;
    private String password;

    public Service(String name, String password) {
        this.name = name;
        this.password = password;
    }

    // getters and setters can be added here
}

Map<String, Service> serviceMap = new HashMap<>();
serviceMap.put("example", new Service("username", "password123"));
// additional services can be added similarly

It's important to note that there may be notable distinctions between JavaScript and Java due to the difference in typing systems.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting the value of abstract bean properties in Spring

I am currently looking for a way to dynamically change the property values of a spring bean during runtime. My current approach involves the following steps: Object bean = context.getBean(beanName); BeanWrapper wrapper = PropertyAccessorFactory.forBeanPro ...

Is it possible to send a search query to thesaurus.com using HTML code on a webpage?

I've created an HTML page with a text field and a submit button. When the user enters a word in the text field and clicks submit, a servlet fetches the input using request.getParameter(). Now, I need assistance implementing functionality to send this ...

Communicating through Socket.io between various Node.js routes

Building a Node.js application that utilizes the Express Framework and Socket.io has presented me with a challenge. I am struggling to receive messages from the server across different express routes. Is there a method for receiving messages sent from one ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

Why aren't methods for JavaScript objects being identified as expected?

My current project involves creating an avatar that follows the movement of my mouse. After successfully developing a functional avatar, I'm now looking to replicate it using an object constructor. The only difference is that instead of var angleToBe ...

Identifying the End of Rendering in Three.js

Is there a way to accurately determine when rendering has been completed? I attempted the following method: scene.add( mesh ); render(); mesh.onBeforeRender = function(renderer, scene){ ... } mesh.onAfterRender = function(renderer, scene){ ... } Ho ...

"Implementing a RoR project using MySQL database on the Heroku platform

Is anyone encountering issues with their Roo project connecting to MySQL for deployment on Heroku? I'm trying to deploy my Roo project using a MySQL database, following tutorials provided by Heroku, but nothing seems to be working. I successfully push ...

What is the best way to include JavaScript in a web view within an Ionic Android application?

I'm in the process of incorporating a header bar into the web view for my app. Utilizing the cordova inAppBrowser plugin to achieve this, I tested using the following code: var win = window.open( URL, "_blank", 'location=yes' ); win.addEven ...

Identifying the Version of JQuery on a Website

Greetings! I recently stumbled upon this website: My curiosity lies in whether it utilizes fullpage JavaScript for the images. I am aware that three JavaScript codes are responsible for the cursor effects, but I am uncertain about how background images ar ...

Tips for avoiding identical selections in multiple drop-down menus within a table and sending the data to the server via a POST request

For instance: visualizing a chart of infants, where each row the user chooses baby GenderDropDown, then picks a name. It's crucial to prevent duplicate name selections throughout the chart. If a user selects Male & John, that choice should not be ...

Filling in form fields with data from the database based on the option selected from the dropdown menu

I'm trying to create a dynamic form where selecting a name from a drop-down menu will automatically populate the rest of the fields with that person's information without refreshing the page. I believe I need to use an onChange function in JavaSc ...

Experiencing issues with the fadeIn() and fadeOut() methods while constructing a slider using Object-Oriented Programming in JavaScript and jQuery

I'm currently trying to develop a slider using jQuery and JavaScript. I have successfully implemented the slide change functionality, but I am facing difficulties in smoothly adding fadeIn() and fadeOut() effects... No matter where I insert these eff ...

Guide to animate/move a tile to a specified location using CSS at 90x90px dimensions

I am trying to achieve a smooth transition for grid[row1][col1] sliding over to grid[row2][col2], followed by an expansion to 90x90px Does anyone have any suggestions on how to accomplish this? Here is an example of the styling: <style type="text/css ...

Tips for transferring input field values through the href attribute in HTML?

How can I pass an integer value from a link to an input field's value? Imagine example.com is a webpage with one input field. I would like to achieve something like this: Click here 1 Click here 2 If the user clicks on click here 1, then ...

Vue-Router: Identified an ongoing redirection loop in a navigation guard while transitioning from the home page to the login page

I need to implement a feature where pages are blocked if there is no authentication token and users are redirected to the login page. I have two .ts pages (main and routes) routes: import { RouteRecordRaw } from 'vue-router'; const routes: Route ...

Guide to verify the appearance of a Facebook popup when clicking a button using Selenium

As I create a step definition for a Cucumber Scenario, in the When clause I click on a button that triggers a popup window for sharing on Facebook. I am wondering whether the driver will automatically focus on the popup window? Or do I need to access it ...

Encountering a problem with JavaScript when coding an Analog Clock

My attempt to create a simple analog clock has hit a roadblock - the hands of the clock aren't moving. I've focused on coding the seconds hand using CSS for testing purposes, but it's not functioning as expected. Here is the code snippet: ...

Incorporating a Favicon into your NextJs App routing system

Encountering an issue with the new Next.js App Router. The head.js files have been removed, thus according to the documentation I need to implement metadata in layout.ts. My favicon file is named favicon.png. How should I specify it within the following c ...

What is the proper way to retrieve an array in the render method using React?

As someone who is still fairly new to React, I encountered a coding issue and stumbled upon this particular code snippet from a previous question (I attempted to reach out to those who had previously answered, but unfortunately, received no response as the ...

Tips for locating an element using Selenium and Java based on its xpath

public void addCustomerInformation() throws InterruptedException { WebDriver driver; WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); driver.get("http://www.way2automation.com/angularjs-protractor/banking/#/mana ...