Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:17:21+00:00 2026-05-27T16:17:21+00:00

This is a simple question (I think) Lets say I have this code (Assuming

  • 0

This is a simple question (I think)

Lets say I have this code (Assuming I have a dog class)

String name = "dog";
dog name = new dog();

How can I get java to recognize name as a String and name the object dog?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:17:22+00:00Added an answer on May 27, 2026 at 4:17 pm

    While you can do what you’re trying in some scripting languages such as PHP (and this question is often asked by many PHP programmers who start Java), this is not how Java works, and in fact variable names are a much less important than you may realize and hardly even exist after code is compiled. What is much more important and what is key are variable references — the ability to gain access to a particular object at a particular point in your program, and you can have Strings refer to objects easily by using a Map as one way.

    For example

    Map<String, Dog> dogMap = new HashMap<String, Dog>();
    dogMap.put("Fido", new Dog("Fido"));
    
    Dog myPet = dogMap.get("Fido");
    

    Or you can gain references to objects in many other ways such as via arrays, ArrayLists, LinkedLists, or several other collectinos.

    Edit
    You state:

    The thing is that in my code I am going to be using one method to create objects, the name of the object is arbitrary but I need it to be dynamic because it wont be temporary, so the actually name of the object has to change or I will be writing over the previously declared object.

    This is exactly what I meant when I said that the name of the variable is not as important as you think it is. The variable name is not the “object name” (this really doesn’t exist in fact).

    For example if you create a dog in a variable named Fido, and then assign it to a new variable named spot, both variables, despite having different names will refer to the very same object:

    Dog fido = new Dog;
    Dog spot = fido; // now fido and spot refer to the same object
    

    If you want to give a variable a “name” consider giving the class a name property:

    class Dog {
       private String name;
    
       public Dog(String name) {
          this.name = name;
       }
    
       public String getName() {
          return name;
       }
    }
    

    Now you can give each Dog object its own (semi) unique name if you wish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty simple question.. lets say I have the following. wordArray WORD
I think this is a simple question, but I can not find the answer
i think this is a simple question but I've searched around and can't seem
I'm hoping this question has a very simple answer. I can think of ways
This is a simple question I think. I am trying to search for the
I think this is a simple question. How do I disable copy from a
I think this is a pretty simple question. How do you an apache rewrite
[see later answer for more] I think this is just a simple rails question,
This is a simple question really. I've been using the new type of constructors
(I don't think I have titled this question correctly - but I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.