Person
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
EXERCISE With me
3 January 2019, 15:43
package com.codegym.task.task02.task0202;
/*
Where does a Person come from?int age, int weight, int money.
*/
public class Solution {
public static void main(String[] args) {
Person person=new Person();
}
public static class Person
{ String name;
int age;
int weight;
int money;
//write your code here
}
}
0
Ritwik Joshi
14 December 2018, 14:36
Person is just another class in the file Solution.java. You can create your own class if you like.
The task is to create a Person object and assign a reference to that object to the variable -> person. When we say - assign a reference to the variable - we simply mean to make it equal to a variable. In Java, variables are stored by reference (or address). So,
means variable a is simply pointing to the location in memory where 2 is stored. It is referring to that location. When you change the value of a, you simply change the location to where a is pointing.
An object is created by using the keyword new. So, to create a Person object we write:
And now to assign a reference to the Person object - which we just created above - to a variable, we simply assign it equal to a variable of type -> Class; and which class? The same class whose object we just created i.e. Person.( Note: type can be primitive or composite. int is primitive type. Class is composite type.)
So we will assign the object reference to a variable which is of type Person class.
Hence,
So, combing the two code steps above, we get:
+2
Angeget
14 December 2018, 10:50
A Person is the class and you create an Object of type Person like : Person p = new Person();
p is a reference to the Person Object.
0