JAVA: INTRODUCTION TO CLASSES
The fundamental concept of object-oriented programming is the class.- A class is the set of instructions that describe how an instance can behave and what information it contains.
- Java has pre-defined classes such as
System
- Way to print out the variable of the class: (In this following case "Store" will be the variable.
public class Store {
// new method: constructor!
public Store() {
System.out.println("I am inside the constructor method.");
String productType;
}
public Store(String product) {
productType = product;
}
// main method is where we create instances!
public static void main(String[] args) {
System.out.println("Start of the main method.");
// create the instance below
Store lemonadeStand = new Store();
// print the instance below
System.out.println(lemonadeStand);
}
}
At the fifth line of the code, there is a string that indicates the field. Add the
String
parameter product
to the Store()
constructor. Then productType
equal to the product
parameter.
Note: Uppercase and Lowercase makes a big difference!! WILL AFFECT THE CODE!!
Store
and assign it to the variable lemonadeStand
. Use "lemonade"
as the parameter value. How to print the instance field productType
from lemonadeStand
.public static void main(String[] args) {
Store lemonadeStand = new Store("lemonade");
System.out.println(lemonadeStand.productType);
}
}
How to connect the field(part 1) and the parameter(part 2) section, part three will be the example part:
public class Store {
// instance fields
String productType;
int inventoryCount;
double inventoryPrice;
// constructor method
public Store(String product, int count, double price) {
productType = product;
inventoryCount = count;
inventoryPrice = price;
}
// main method
public static void main(String[] args) {
Store cookieShop = new Store("cookies", 12, 3.75);
}
}
What I learned in this class...
Classes define the state and behavior of their instances. Behavior comes from methods defined in the class. State comes from instance fields declared inside the class.
What I'll be doing tomorrow...
I will be learning LEARN JAVA: METHODS
hi
ReplyDelete