CodeGym /Courses /New Java Syntax /Different ways to create variables

Different ways to create variables

New Java Syntax
Level 8 , Lesson 4
Available

"Hi, buddy. I made a copy of your contract for you, just in case. Rishi, that cheapskate, is blissfully ignorant. You should see the figures in my contract. Ha!"

"Good job, Diego. I think I'll learn a lot from you."

"Sure thing, Amigo. There are too many stupid people in the world who want to get rich without actually doing something. But there are even more idiots who are ready to work for free."

"OK, let's get back to our lesson. Now I'm going to teach you several ways to create variables:"

Example Explanation
String s1 = new String();
String s2 = "";
Create two identical empty strings.
int a;
Create an int variable;
int a = 5;
Create an int, variable named a and set its value equal to 5
int a = 5, b = 6;
Create an int, variable named a and set its value equal to 5 Create an int, variable named b and set its value equal to 6
int a = 5, b = a + 1;
Create an int variable named a and set its value equal to 5 Create an int variable named b and set its value equal to 6
Date date = new Date();
Create a Date object. It is initialized to the current date and time.
boolean isTrue = true;
Initialize a boolean variable to true
boolean isLess = (5 > 6);
Assign false to the isLess variable. Boolean variables only accept the values true and false.

"Cool, Diego! You always make everything so clear."

"LOL! Thanks, Amigo."

"By the way, I have a couple more exercises for you. How are they going so far?"

"They weren't too hard, and some were pretty funny."

8
Task
New Java Syntax, level 8, lesson 4
Locked
Variety
In the Solution class, create six variables: - declare two of them using separate statements, and assign them some values immediately upon declaring them; - declare two variables on one line, and do not assign anything to them; - and declare two more variables on one line, and immediately upon decla
8
Task
New Java Syntax, level 8, lesson 4
Locked
Crazy eights
Create 10 Cat variables and 8 Cat objects.
8
Task
New Java Syntax, level 8, lesson 4
Locked
Every animal should have an owner
Create Cat, Dog, Fish, and Woman objects. Assign an owner to each animal.
Comments (124)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Lishon Level 3, Nottingham , United Kingdom
8 July 2023
Did better on this section.
Fadhil Radhian Level 18, Semarang, Indonesia
13 March 2023
funny and clear as usual, great article !
Miguel Alonso Ramos Alarcon Level 3, Lima, PERU
16 February 2023
Cat cat9; Cat cat10; Cat cat1 = new Cat(); Cat cat2 = new Cat(); Cat cat3 = new Cat(); Cat cat4 = new Cat(); Cat cat5 = new Cat(); Cat cat6 = new Cat(); Cat cat7 = new Cat(); Cat cat8 = new Cat();
Anonymous #11307698 Level 3, Korea, Republic of
7 March 2023
nice
Roman Bortnyk Level 10, Italy
19 April 2022
Hint: The animal.owner can assign woman only after line of the woman obj was created. So you can't assign dog.owner = woman; before the Woman object was created.
Nikita Petrov Level 30, Astrakhan, Russian Federation
20 March 2022
I'm not sure if

String s1 = new String();
String s2 = "";
from the example creates two identical empty strings. "s2" is not filled with any symbols (empty), but "s1" is "null", it's not initialiezed with any string, even empty.
jrcv Level 3, Spain
28 December 2021
aldo en los metodos public estamos declarando la variasbles women y owner por lo que se utiliza public woman owner para cada metodo o objeto dog,fish y cat
Aldo Luna Bueno Level 1, Peru
7 November 2021
Woman woman = new Woman(); cat.owner = woman; Bueno, en estas dos líneas entiendo que pasa lo siguiente. En la primera declaramos la variable woman de tipo Woman, creamos un objeto del mismo tipo, y la variable woman almacena la referencia a este objeto (no el objeto en sí) gracias al operador de asignación (=). En la segunda línea la variable owner de la clase cat, a la cual se accede usando el operador punto con cat.owner, copia la referencia que guarda la variable woman. En conclusión, ahora tenemos dos variables que hacen referencia al mismo objeto, pero el objeto sigue siendo uno solo.
MsebaF Level 10, San Luis, Argentina
24 October 2021
ok, i did understand the excersice but can someone explain this please public static class Cat { public Woman owner; } why is "public Woman owner;" inside the Class cat?
Anonymous #10604401 Level 8, Skopje, Macedonia
28 December 2021
You're creating a variable named owner inside the Cat class. the variable is of Woman type, i.e. the owner variable can only be assigned a value/variable from the class Woman. So if you have other classes, say car, house etc., it would mean only an object of the woman class can be owner of a cat object. House and car obviously cannot be owners of a cat.
MsebaF Level 10, San Luis, Argentina
31 December 2021
thanks!
AaronC90 Level 2, Managua, Nicaragua
27 July 2021
hola alguien me puede ayudar con la ultima tarea por favor.
Anonymous #10774868 Level 2, San Salvador
29 July 2021
Hola: debes crear los objetos desde cada clase estática así: Gato gato=new Gato(); gato.propietaria=mujer; y así para cada uno de los objetos(animales). Todo eso debes ponerlo dentro del método principal en un solo bloque.
Bak Level 5, Kansas City, United States
16 December 2020
Though I was able to input the right code and pass this problem "pets need people" (task0213), I'm not sure what this means: cat.owner=woman; dog.owner=woman; fish.owner=woman; By writing the above code, are we putting an entire object "woman" into a variable of any given object? Or are we making a reference from a variable (cat.owner) to an object? Thanks.
Ahmad Level 7, Detroit, United States
29 December 2020
You are making a reference from variable (cat.owner, dog.owner and fish.owner) to object woman.
Bak Level 5, Kansas City, United States
4 January 2021
thanks
Anonymous #10836151 Level 3, Durham, India
28 September 2021
You are setting woman as value to property owner of Cat,Dog,Fish.