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

Good to hear that you resolved it, do you also understand it?

I personally don’t think Java is a good first language to learn, due to all the complicated OOP. Sure, you will need to learn sooner or later, but preferable a bit later

but that is just my personal opinion.

1 Like

That code should be:

public class Main {
    public static void main(String[] args) {
        System.out.println("lemonade");
    }
}

If one sticks to static methods until one actually has some kind of reason for creating a type, then java is totally fine to learn programming with, probably pretty good, even. But yes, one might want to avoid it because of tutorials’ perverse use of classes.

The reason I imagine it’s good for learning is that the static type checker makes the user think about how things fit together. It also communicates with things using methods, which is less special than python’s many operators, it’s more explicit, difficult to accidentally get something to “work”

…that said, golang might do all of that better, and has a community that values simplicity, doing away with non-essential things.

@noeman5 please take note of that the code you have there uses Store as a string. If a string is needed, use a string. That’s half the code gone, it didn’t do anything. And there’s no reason to create a variable whose value will be used once, not in this case anyway, so just use the string literal … and then you’re left with code where each thing has purpose. If something doesn’t have purpose then please remove it. Prune away things that do nothing, including things that you don’t know what they do, because if you don’t know what something does then it has no place in your code.

Not my first language, that was Visual Basic about 30 years ago,(back in the pre-dot net days), since then I’ve dabbled with Python, not a compiled language or OOP. Lots of new things to try to wrap my head around, trying to get the relationships and terminology down. Thanks for the help.

1 Like

Hello!
I know that you can say

this.productType = product;

and

productType = product;

What’s the difference between these? I played with them and I got the same results.

1 Like
this.productType = product;

By typing .nameOfTheField, you are accessing to the field of your instance.

productType = product;

The constructor is assigning the argument product to the attribute productType.

2 Likes

Anyone knows what the ■■■■ that sentence means?
“Our method also has a One instance may have This class has a An instance is the thing itself.”
I am not native speaker and for me that sentence is the most stupid sentence I have ever read.

1 Like

In the introduction to classes, I don’t see the sentence highlighted by you in yellow.
Can you provide a link to the page where you found the above sentence?

1 Like

… I wish I could. Yesterday I saw that sentence on each page of Introduction to classes. But I can’t see it anymore. I swear to God, it was there xD
But I recovered 4 screenshots I have made yesterday: Imgur

1 Like

public class Store {
// instance fields
String productType;

	  // constructor method
	  public Store(String product) {
	    productType = product;
	  }
	  
	  // main method
	  public static void main(String[] args) {
	    Store lemonadeStand = new Store("lemonade");
	   System.out.println(lemonadeStand.productType);
	  }
}

}