Click here to Skip to main content
15,924,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow all i first asked a question here
Requesting help regarding proper coding designs[^]

maybe i am just a fool but i could not catch up with what they had mentioned there all though i did some research and came into a conclusion where i think that a static variable is something that we assign to a variable when that is common to all the objects . However i am still stuck with how i should access variables that are in one class from another .(also pls correct me if my idea on static variables is wrong)This is a code that i just came up with to make the question more easier to understand

package testing1;

import java.util.Scanner;

class HerDetails {

    public String her_Name = null;
    public int her_age = 0;
    public String her_eye_colour = null;
    private String her_Hair_Colour = null;


    public void getHerDetails() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Her name");
        her_Name = sc.next();
        System.out.println("Enter her age");
        her_age = sc.nextInt();
        System.out.println("Enter her eye colour");
        her_eye_colour = sc.next();
        System.out.println("Enter her hair colour");
        her_Hair_Colour = sc.next();


    }

    public void herDetails() {

        System.out.print(her_Name);
        System.out.println(her_age);
        System.out.println(her_eye_colour);


    }
}

class  HisDetails{

        public String his_Name=null;
        private int his_Age;
        private String his_eye_colour;



        public void getHisDetails(){
        Scanner sc1=new Scanner(System.in);
            System.out.println("Enter His name");
            his_Name= sc1.next();
            System.out.println("Enter His Age");
            his_Age=sc1.nextInt();
            System.out.println("Enter his eye colour");
            his_eye_colour= sc1.next();



        }
        public void displayHisDetails(){
            System.out.println(his_Name);
            System.out.println(his_Age);
            System.out.println(his_eye_colour);

        }



}




public class Main {

    public static void main(String[] args) {

         HerDetails h1=new HerDetails();
         HisDetails h2=new HisDetails();
        h1.getHerDetails();
        h2.getHisDetails();
        h1.herDetails();
         h2.displayHisDetails();

    }
}


so how do i go about doing this imagine i wanted to create a class called "theirdetails" where i am going to insert data like
int theDayTheyMet
and
int ageDifference
or String
theirNamesTogether
which i think would make sense to you hopefully . So inorder to go about this i clearly need to use the ages of the two people to find the difference from the other two classes and their names to get their names together . How do i get those values entered from the first class to another class ? i want to know the correct way?

What I have tried:

Im not sure iv read alot but not had any luck i know that setters and getters work , but is that what i should use remains a doubt ? meaning i still dont know my basics.? please enlighten me
Posted
Updated 11-Apr-18 21:17pm
v3

1 solution

Java is OOP[^] language and therefore you can use inheritance, polymorphism[^], etc.
An example from above link:
Java
class Teacher {
   String designation = "Teacher";
   String collegeName = "Beginnersbook";
   void does(){
	System.out.println("Teaching");
   }
}

public class PhysicsTeacher extends Teacher{
   String mainSubject = "Physics";
   public static void main(String args[]){
	PhysicsTeacher obj = new PhysicsTeacher();
	System.out.println(obj.collegeName);
	System.out.println(obj.designation);
	System.out.println(obj.mainSubject);
	obj.does();
   }
}


On the same manner, you can extend Person class by creating Man and Woman classes which derived from Person.

As to this statement:
Quote:
i wanted to create a class called "theirdetails" where i am going to insert data like
int theDayTheyMet
and
int ageDifference
or String
theirNamesTogether


You can always create a Couple or Pair class which may hold 2 public members: Man and Woman.

Java
class Couple {
    private Man his = new Man(...);
    private Woman her = new Woman(...);

    //another members, methods here
    
}

Because Man and Woman base class is Person, you can simplify your Couple class to:
Java
class Couple {
    private List<Person> persons = new ArrayList<Person>();

    //another members, methods here
}

A List[^] and an ArrayList[^] provide several methods to add/iterate/remove elements.

[EDIT]
I'd store date[^] of birth instead of age. You can calculate age this way: year_of_current_date - year_of_birth.
See:
Calculate Age from date of birth in Java - Memorynotfound[^]
date - How do I calculate someone's age in Java?[^]
 
Share this answer
 
v3
Comments
CPallini 12-Apr-18 3:42am    
5.
Maciej Los 12-Apr-18 3:47am    
Thank you, Carlo.
Member 13771743 12-Apr-18 10:14am    
Thank you alot but i wasnt worried about finding the age i just wanted to know how to use variables from one class to another what is the correct way of doing so how bout setters and getters what role do they play
Maciej Los 12-Apr-18 11:23am    
Well, you did ask about design, you didn't mention about getters and setters in your question...
Member 13771743 12-Apr-18 12:31pm    
good point my bad, thank you for that even though what i really want to is how to access one class variables through another as i have shown in the above example
do i need to creat an object of that in order to use it inside another class

i found out by making it "public static" within the class (not inside a method) makes it able to use it in any class by String nm1=Class.Attribute() is it correct to make it public static just so i can use it in any class? if now how can i access a variable in one class from another tht has a value assigned to it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900