FAQ: Java: Introduction to Classes - Classes: Constructors

This community-built FAQ covers the “Classes: Constructors” exercise from the lesson “Java: Introduction to Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Classes: Constructors

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In exercise 3/8, why does it display Store@6bc7c054
in the console after running the code?

5 Likes

Hi betarockstar, did u get the answer on your question?

I going to assume he did but, why does that show up?

1 Like

Please explain the difference between Constructors and Methods

1 Like

Hi betarockstar69942 - Store refers to the name of the class and @6b7c045 refers to the memory.

1 Like

Can anyone tell me what instances mean? In a simplified way

Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

https://www.tutorialspoint.com/java/java_object_classes.htm

Kinda roundabout but this helped me get a better Idea of what it is

Hello everyone, I can’t understand why the string with the name of the class and the corresponding memory is printed.

This action is not specified in the program and also in the lesson…

Lastly is possible have the same result without this line?

Thank you!

1 Like

What is the use of ‘new’ in this program?

I’m don’t quite understand why “I am inside the constructor method.” was printed out.
I believe I only created a new instance and did not ask anything to be printed out in Main.

1 Like

When you create a ‘new’ instance of a class, the code inside the constructor method is executed. The constructor for the ‘Store’ class only has one line of code. That line prints the message you referred to. The exercise was only illustrating how the control flow is passed around inside a class. The next exercise will cover how constructor methods are typically used to create an instance of a class.

4 Likes

Thanks for the reply! It’s starting to click.

2 Likes

Hi!

Is there a table / map / comparison available to understand the following words and differentiate between them?

  • class
  • constructor
  • instance
  • method
  • object
  • type
  • variable

Thank you for the help!

2 Likes

I don’t understand any of this lesson, can someone please try to explain it to me? I don’t really get what classes or instances are, and the constructor method is really confusing me.

2 Likes

Can someone explain what is Method and Constructor? What is the difference between them?

@coolgirl123
@aydanshirin

Hey guys, I’m very new to this so I may be wrong but my understanding of classes and instances, methods and constructors is this;

Class: Think of the class as a box. Within a program your coding you might have 10 boxes. Each box is used to define and do different things within the program. (For example, if your writing a program for a university you might have a class for students, a class for teachers, a class for courses, a class for alumni, etc.) So a class is really a set of data and instructions within your code that relates to one thing. (Students, teachers, etc)

Instance: So if we have a class for students we’ll need to store data in this class about each student at the university. Lets say the data we have for each student consists of their name, age and exam results. Within the CLASS we’ll need to our data as variables. (string for name, int for age and double for exam results) Within the METHOD we then create instances. Think of the instance as each individual student at the university. The variables are student name , age and exam results whilst the instance is the individual student. (i. .eTom Smith, 21, 71.23) If you had 100 students at the university you’d have 3 variables (student name, age, exam results) and 100 instances. (1 for each student)

Method & Constructor: Think of the method the same way as you would a cooking recipe. The method tells you what to do with the ingredients. The ingredients are our instances. Now when we read a recipe we as humans know what the ingredients are and what the amounts mean. (e.g. 2 eggs, 300g flour, 200ml milk - we know what all of these things are so all we need are the different ingredients and we can follow the method to make our meal) Computers don’t know what the ingredients (instances) are! So even if we enter our different instances (i.e. data) into our code and give perfect instructions of what to do with the instances it won’t know what egg, flour or milk means. This is why we have a constructor. The constructor is created within the class to tell the program what an egg is, what milk is, etc.

In the previous example of the university within our class of students we have 3 variables (student name, age, exam results) and 100 instances (one for each student) Just like the cooking recipe the program doesn’t know what a student name, age or exam result is. So we make a constructor to tell the program the data type for each variable. (Student name is a string, age is an integer and exam result is a double) Now that we’ve defined each variable’s data type in the constructor, when the program takes each instance (individual student) we enter into the code and applies the set of instructions we’ve outlined in the method, it knows what the data actually means! (because of our constructor)

Hope that helps! Probably made more questions than gave answers but hopefully there’s something in there that makes sense. Also please if any of the above is incorrect (which is very well may be) please feel free to point out where I’m wrong. I’m amateur and learning!

17 Likes

how does the line Store lemonade = new Store() work ? Like what happens during the execution of this line and how does the print statement in the constructor gets printed using this code?

Can anyone please explain why we are getting the below
Store@2aae9190
Store@2f333739
Store@77468bd9
Store@12bb4df8

I run the code below in main. Someone said that is showing memory, but what memory are we referring to?. Thanks for your help.

Store lemonadeStand = new Store();

Store fruit = new Store();

Store banana = new Store();

Store orange = new Store();

System.out.println(lemonadeStand);

System.out.println(fruit);

System.out.println(banana);

System.out.println(orange);

new represents a keyword that is used to tell the program that a new instance is created