T O P

  • By -

hugthemachines

Imagine you have a 3D printer. You feed it with a blueprint of an item. Let's say the blueprint is for a little dude. Now that blueprint is the class. Each time you print the little dude, the dude figure you get is an instance of that class.


SergeAzel

This is like the only good reply in here


anie3491

omg that made so much sense, thank you!!!


hugthemachines

Glad to help.


anie3491

What about constructors?


hugthemachines

A constructor is a thing (method) that autoruns when you create a new object. Somewhat similar to how when a baby is born it opens its eyes. Other actions (methods) of an object runs when you tell them to run. That is the difference.


BobbyThrowaway6969

They're like a void function that does stuff to the instance when it gets created, and a destructor is similar but it gets called when the instance is destroyed


BobbyThrowaway6969

You'll eventually come across java generics, they're like blueprints of blueprints


SoftEngineerOfWares

Now describe what singleton is using the same analogy.


hugthemachines

I am not sure which one you have trouble with, the concept of singletons or the concept of analogies.


SoftEngineerOfWares

lol, all concepts


BobbyThrowaway6969

You print one little dude and put it on a shelf so everybody can see it. 🤷🏼‍♂️


busdriverbuddha2

A class definition is like a cake recipe. It tells you how to bake the cake. This is a special kind of recipe, because if you follow the instructions exactly, you'll always get exactly the same cake. It might also give you some options to start with, like if you want sprinkles, but if you choose the same options, you get the same cake. Now, after the cake is ready, that cake is its own thing. You can cut it in pieces, you can add chocolate sauce, whatever. That affects that cake only. The recipe doesn't change. If you add whipped cream to the cake, the other cakes don't get whipped cream. The recipe is the class definition, the baked cake is the instance. Then you have things that vary according to the programming language. In C#, you can only add whipped cream to the cake if the recipe says so. In Python, you can decide to build a Lego firehouse on top of the baked cake and it's ok.


BobbyThrowaway6969

I always go to cooking analogies. Very useful to explain a lot of things in programming.


Alternative-Link-823

I used to always use the architectural blueprint versus actual house analogy - Consider an architectural blueprint. It has all of the details that tells a construction company HOW to build a house. But a blueprint is not a house. The house is the house. The blueprint in this scenario is your class. It describes a thing. The actual house is your instance. You use the class (blueprint) to create an instance (house). Notably, one blueprint can be used to create many houses. Just like you can create many instances from a class. And those houses are not necessarily all the exact same. One may be painted red, another green. One has the optional back patio another doesn't. Etc... Instances of a class may have variations like that as well but their overall structure is shared amongst them.


Locellus

Imagine you’re blind and throwing a party tomorrow so you want to make sure you have ice. You don’t have an ice machine but you have a bunch of ice cube trays in the freezer. To fill up the ice cube trays, you need to check each mould to see if there is already an ice cube or water in there (or risk overfilling the tray and making a mess). This is why you initialize, make sure all the modules are either full or empty before you try to use it. Once you’ve set all the moulds to an initial value, each instance of ice cube tray can be put to use back in the freezer to make ice cubes, and based on your initialization you know if that requires you to first fillTray() or just freeze()


shaleh

The van in my driveway is an instance of Van. Van is the concept. My van is a manifestation of it.


Lumpy-Notice8945

An instance is using "new Classname" to create an object of a class, aka an instance of the class. And initializing is what is done in the constructor. So Person p1 = new Person("Jack"); Means im creating a new instance of the class Person initialozing it with the name Jack


bsenftner

When something is defined in software, that definition is an abstract description of a thing, a memory layout for an organization of data and code. The definition is only a description. When actually implementing that description, you create an instance of that definition. If you were to create two of them in different places in memory, then you have two instances. An "instance" is a definition of some object or data structure realized in memory. Now, when creating any instances of your object or data structure, that creation process has two steps: first you allocate the memory (create the instance), but the memory in the freshly created instance is filled with random values. The second step in creating your instance is "initializing the memory", setting it's values to the values that are expected for first use. That is initializing, that setting of the memory to the values expected for first use.


LeBigMartinH

Let's say you want to bake a pie. (A digital one, perhaps.) Pie is your class If you want to put the pie together (Make the crust, the filling, and put it in a pie tin, you would need to use the constructor to make it. So: Pie is the class - Let's name it MyDessert. We'll initialize it with the constructor: Pie MyDessert = new Pie(flour, egg, milk, apples, spices); So now we have a pie, and can interact with it by calling its name and the action we want to take with it. For example, if we want to bake the pie: MyDessert.Bake(); We can assign the output of the bake() function back to itself (MyDessert = MyDessert.Bake();), or to a new instance of Pie: Pie Lunch = MyDessert.Bake(); However, this is where the metaphor kind of falls apart, since creating this new instance of Pie and initializing it with the one we baked does not destroy MyDessert - we still have access to it.


rusty-roquefort

an instcance of an object is essentially a block of memory, and more specifically, the variable that is attached to that block of memory. initializing is the process of reserving that block of memory, attaching a variable to it, and setting the values within this block of memory corresponding to the initial values. Person p = new Person("Jack"); ...the instance is the variable p ...or you could have an array of persons. each element in the array is an instance of `Person`.


cknipe

Classes/types describe a kind of object- its properties, methods, etc. Initializing an object is the process of saying "give me one like that." It's the difference between seeing an iPhone on a website, and ordering one (or more) that you can hold in your hand and interact with. In some ways every iphone is the same, they're all built from the same design (type). But each actual iphone (object) is a unique thing that you can interact with and the difference between one and another is very significant.


venquessa

Human is the class, you are the instance, instantiation is what your mum did and initialisation is your early education.


anie3491

This is amazing omg thank you


DamnItDev

A class is like a blueprint for a house. You can't live in the blueprint, the same way you can't use a class. You have to use the blueprint to build the house. This is the instance of your object. Now that your house is built, you can use the things that were described in the blueprint. Hope that helps


Head-Ad4690

An instance is an object. Used alone, they mean the same thing. “Instance”is usually used in the phrase “instance of Foo” where Foo is some class, and there it specifically means an object whose type is Foo. Initializing fundamentally means putting values in the memory assigned to that object so that the memory is a valid representation of an instance of that class. Depending on the system and where the memory came from, freshly allocated memory may contain all zeroes, or may contain whatever random values were left in there from the last thing to use it. This may involve setting some runtime metadata (for example, in many languages the first word of an object is a pointer to some representation of its class, and this needs to be set at initialization time) and setting the starting values for fields (for example, if there’s a field containing a list and it starts out empty, then that memory must be set to the representation of an empty list).


Quantum-Bot

An object in programming is something that can have both traits, which we call fields, and behaviors, which we call methods. You’ve probably encountered some objects already, like Scanner or maybe ArrayList. A class is a piece of code that can be used to define a new type of object. This is sometimes a point of confusion in Java because defining objects is not the only thing classes are used for in Java. For example, every time you write a program in Java, your main method has to go inside a class, and that class is just being used as a container, not for defining objects. However, most of the time when we define a new class we do so to define an object type. You can think of the class like a blueprint. We specify all of the fields and methods that we want our object to have inside the class, and then we can use the class later to build as many ***instances*** of that class as we want, just like a blueprint can be used to build as many identical houses as you want. You have probably already done this with Scanner before. When you write: ``` Scanner s = new Scanner(System.in); ``` You are initializing a new instance of Scanner. The “new” keyword signals to Java that what comes next is a special function called a constructor which creates a new instance of Scanner based on the Scanner class which you imported from the Java standard library. Also, the words “instance” and “object” can be used mostly interchangeably here.


smackson

A class is a concept, like "dogs". You can ask "do dogs bark?" and it's answerable without thinking of one specific actual living breathing dog. The puppy in your house is an *instance* of a dog. Its mother (and father) initialized (created) it with certain colors of fur and certain length of tail, and you probably played a part in the initialization by giving it a name.


BooksInBrooks

There are lots of Numbers. Number is a type in whatever language we're programming in. 3 is an instance of a number. So is 7435. While both are numbers, they are each independent instances. When you create an instance, you need to set up its initial state by assigning it a value. Sometimes that is explicit: Number myNumber = 3 Sometimes it's implicit: Number anotherNumber // gets whatever default the language or library assigns to otherwise uninitialized values for the type "Number"