Java is statically typed: every variable is declared with a type that never changes. int age = 36; holds a whole number, double height = 1.7; a number with a decimal point, boolean open = true; a yes or no, char grade = 'A'; one character in single quotes, and String name = "Ada"; text. Assigning again replaces the value, but only with a value of the same type; age = "old"; does not compile.
Variables declared inside main live only inside it. A variable declared at class level with static belongs to the class and can be read from anywhere as Main.name; the checks in the first lessons read your values that way. Names are camelCase: totalPrice.
A Java file can hold more than one class. This course ends in a project, a small library system, and a few lessons along the way build one of its classes; the first is Policy, two constants marked static final, which means they can never change.
public class Main {
static String city = "Lisbon";
public static void main(String[] args) {
int population = 545000;
city = "Porto";
System.out.println(city + " " + population);
}
}Your task
Declare three static variables on Main: name (a String with any text), age (an int), and height (a double). In main, print <name> is <age>, for example Ada is 36, by joining the variables with +.
Then add the class Policy after Main with two constants: static final int LOAN_DAYS = 14; and static final int DAILY_FEE_CENTS = 25;.
Part of the final project. Policy holds the loan length and the daily late fee the library charges. It is saved in this browser and loaded into Project: the library when you get there.
Hint 1
static String name = "Ada"; goes inside Main, above main. Text needs double quotes; numbers do not.
Hint 2
System.out.println(name + " is " + age); joins text and numbers. The Policy class body is just the two static final lines.
Show the solution
public class Main {
static String name = "Ada";
static int age = 36;
static double height = 1.7;
public static void main(String[] args) {
System.out.println(name + " is " + age);
}
}
class Policy {
static final int LOAN_DAYS = 14;
static final int DAILY_FEE_CENTS = 25;
}