Search code examples
javabluej

using two classes in java


I am trying to use two classes Member class and Website class to talk to each other. I want to use the code which is in the setloggedInStatus() method in the Member class and be able to use it in the memberLogin() method which is in the Website class. I used the Member memberObject = new setloggedInStatus(); code but its giving me an error.

I Would appreciate any help. Thanks in advance

Website class

public class Website
{
    // declaration of vars 
    private String websiteName;
    private int hits; 
    private double salesTotal; 
    

    /**
     * Constructor for objects of class Website
     */
    public Website(String websiteName)
    {
        // initialise instance variables
         this.websiteName = websiteName;  
    }
    
   
    Member memberObject = new setloggedInStatus(); 
 
   public void memberLogin() {
       
    
    
    }
}

Member class

public class Member
{
    // varibales declaration 
    private String email;  
    private int membershipNumber; 
    private boolean loggedInStatus; 

    /**
     * Constructor for objects of class Member
     */
    public Member(String memberEmail, int newMembershipNumber )
    {
        // initialise instance variables
        email = memberEmail;
        membershipNumber = newMembershipNumber;
    }

    //loggedInStatus method
    public void setloggedInStatus() {
    if (email != null && membershipNumber != 0) {
        loggedInStatus = true;
        System.out.println("you are logged in ");
         
    } 
    
    else {
        loggedInStatus = false;
        System.out.println("you are not logged in");
    }
}
    
  
    }



Solution

  • If you wanted to use the functionality of Member inside the class Website, you would need to import it via import Member at the top of the Website file (depending on if they're in the same folder/package). This will make it available inside the file.

    You could then create a new Member object via Member member = new Member(params go here);

    Then, you could call the methods contained inside your Member class from your created member object, for example member.setLoggedInStatus();

    EDIT: Did this answer your question?