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

  • SEARCH
  • Home
  • 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 9100729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:56:21+00:00 2026-06-17T00:56:21+00:00

I know several programming languages. Most of them are scripting languages like lua, perl,

  • 0

I know several programming languages. Most of them are scripting languages like lua, perl, JS, ruby, etc.

But recently, I started programming in Java, which works quietly. So I have been thinking of a certain function that exists in JS. The prototype of constructors, that is. For further understanding of what my question really is, I will make an example in JS. Let’s say you want to create an application of dogs.

function dog (){
this.property1 = value;
this.propertr2 = value2;
this.propertyN = valueN;
//etc.
}

//now, I will create several instances of the constructor in JS

var snoopy = new dog();
var buddy = new dog();

and the awesome part, that I know about JS is that you can dynamically change the information of the constructor and all of the instances that is of the constructor (as it is called in JS) with the prototype keyword like this:

 dog.prototype.bark = function () {
 console.log("Woof!");
 };

and THIS, does not only change the information about the constructor so that every dog that will ever be created with the constructor will know how to bark, it also changes so that all of the instances of the constructor gets the information of the prototype insertion which in this case teaches the dogs how to bark. which we can see in the next example:

var someOtherDog = new dog ();
someOtherDog.bark(); //output will be: Woof!
snoopy.bark();       //output will also be: Woof!
buddy.bark();        //you guessed it! it will also be: Woof!

So with this prototype keyword in JS I can manipulate constructors and their instances. Now, my question is:

HOW can I manipulate the classes and their instances in java? And is that even possible?
and if so; what should I do in order to do anything like that in java?

class dog
{
    private String hairColor;
    public dog ()
    {
        hairColor = "Brown";
    }
    public static void main (String args[])
    {
        dog snoopy = new dog ();
        dog buddy = new dog ();
        //now, how do I manipulate the class like I did in JS?
    }
}
  • 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-06-17T00:56:22+00:00Added an answer on June 17, 2026 at 12:56 am

    You can create anonymous classes on the fly if you want.

    Say you have a class:

    class Dog {
        String name;
        Dog(String name) { this.name = name; }
        void bark() { System.out.println(name + " says 'woof!'"); }
    
        public static void main(String...args) { 
            Dog snoopy = new Dog("snoopy");
            snoopy.bark();
        }
    
    }
    

    Here’s the result

    c:\files\j>javac Dog.java
    
    c:\files\j>java Dog
    snoopy says 'woof!'
    

    Now buddy he doesn’t say woof – he says ruff! So we create one on the fly like so

    class Dog {
        String name;
        Dog(String name) { this.name = name; }
        void bark() { System.out.println(name + " says 'woof!'"); }
    
        public static void main(String...args) { 
            Dog snoopy = new Dog("snoopy");
            snoopy.bark();
            Dog buddy = new Dog("buddy") {
                @Override void bark() { System.out.println(name + " says 'ruff!'"); }
            };
            buddy.bark();
        }
    
    }
    

    Which results in

    c:\files\j>javac Dog.java
    
    c:\files\j>java Dog
    snoopy says 'woof!'
    buddy says 'ruff!'
    

    If you wanted to permanently change every dog, that becomes more difficult, but can be done via the strategy pattern.

    Let’s say we have the following

    abstract class BarkingStrategy {
        public abstract void doBark(Dog dog);
    }
    
    class TypicalBarkingStrategy extends BarkingStrategy {
        public void doBark(Dog dog) { System.out.println(dog.getName() + " says 'woof!'"); }
    }
    
    class AggressiveBarkingStrategy extends BarkingStrategy {
        public void doBark(Dog dog) { System.out.println(dog.getName() + " says 'Rrrruff!'"); }
    }
    
    class Dog {
        // notice this is static - that means it belongs to the class itself, not 
        // any particular instance of it - similar to prototype
        static BarkingStrategy bark = new TypicalBarkingStrategy();
        String name;
        Dog(String name) { this.name = name; }
        String getName() { return name; }
        void bark() { bark.doBark(this); }
    }
    

    Then you can do the following

    public static void main(String...args) {
        Dog snoopy = new Dog("snoopy");
        snoopy.bark();
        Dog.bark = new AggressiveBarkingStrategy();
        snoopy.bark();
    }
    

    This results in

    c:\files\j>javac Dog.java
    
    c:\files\j>java Dog
    snoopy says 'woof!'
    snoopy says 'Rrrruff!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what programming languages do the most famous and lucky
I've been programming in Linux/UNIX for several years now, but recently I needed to
I know several programming languages including Objective-C, Java, C#, and python, and C. However,
I recently got a job working for a company who uses several programming languages
I know there are several threads asking similar questions - but I havent found
I know there are several questions about satellite Assembly's out there, but I still
I know there are several libraries to connect to XMPP servers, but is there
I know this has been asked different ways several times, but I'm just not
I know this question is similar to several previous ones, but I can't find
Programming languages (e.g. c, c++, and java) usually have several types for integer arithmetic:

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.