FAQ: Java: Introduction to Classes - Classes: Instance Fields

This community-built FAQ covers the “Classes: Instance Fields” 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: Instance Fields

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!

How do we “Add some state to our store class”?

its mention in the next line:

Declare a String instance field for productType

the code also provides where you need to set the instance field:

// declare instance fields here!

and you have an example you can follow:

public class Car {
  /*
  declare fields inside the class
  by specifying the type and name
  */
  String color;

  public Car() {
    /* 
    instance fields available in
    scope of constructor method
    */
  }

  public static void main(String[] args) {
    // body of main method
  }
}
1 Like

How do we avoid printing the memory location and constructor method name?

1 Like

Hi. I have a question of the lesson ’ Classes: Instance Fields’
Why does main method print before constructor method as code below;

public class Store {
// declare instance fields here!
String productType;

// constructor method
public Store() {
System.out.println(“I am inside the constructor method.”);
}

// main method
public static void main(String args) {
System.out.println(“This code is inside the main method.”);

Store lemonadeStand = new Store();

System.out.println(lemonadeStand);

}
}

/*
prints:
This code is inside the main method.
I am inside the constructor method.
Store@2aae9190
*/

Thank you!

the main method is used by JVM (java virtual machine), so that runs first.

then within the main method, you create a class instance which triggers the constructor

Why don’t we declare intsance fields within the constructor ?

Because that isn’t logical. Methods (including the constructor) have local scope, we want to be able to access the instance fields of a class, as such, they are declared in the class

What does adding the “new” in front of things do?

Welcome to the forums!

new is a keyword that creates a new object. Find more about it here.


Example

Car myCar = new Car();

In this example, we use new to create a new object, then call the Car() constructor method to build that object. In the end, we have a new instance of the Car class that is saved to the variable myCar.