Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4333218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:21:47+00:00 2026-05-21T10:21:47+00:00

I would like to know how to use multiples clas in Java. I know

  • 0

I would like to know how to use multiples clas in Java. I know how use method from other class and also constructors but i would like know how create a new “object” for example. If i am making a PersonDirectory, then i can have class called Person which has the attributes Name and Age. Then i want to make a Person[] in my PersonDirectory Class and add names and ages to it. How can i do that? I have some code that i did, but it doesn’t seem to work out.

import java.io.*;

public class PersonDirectory {

    static BufferedReader br = new BufferedReader
        (new InputStreamReader(System.in));

    static Person[] personArray = new Person[2];

    public static void main(String[] args) throws IOException{

        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please enter the name of the person: ");
            String name = br.readLine();
            System.out.print("Please enter the age of the person: ");
            int age = Integer.parseInt(br.readLine());
            personArray[i] = new Person(name,age);
        }

        for(Person p : personArray) {
            System.out.println("The name is "+p.getName()+" and the age is "+p.getAge());
        }

    }
}

second class

public class Person {

    private static String name = "";
    private static int age = 0;

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

    public static String getName() {
        return name;
    }


    public static int getAge() {
        return age;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:21:48+00:00Added an answer on May 21, 2026 at 10:21 am

    This is because the properties in the Person class are static. Static means that they are shared between all object(instances). Remove the static keyword from the Person class and you will be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC Preview 4 and would like to know how to use
I would like to know what kind of tool you use for writing your
I would like to know how to make use of an Axiom database resource
I have a generator and I would like to know if I can use
Does anyone here use Spec# regularly? I would like to know if it is
Accuracy Vs. Precision What I would like to know is whether I should use
I would like to know how can I use jQuery to select multiple items
I would like to know what data structure / storage strategy I should use
I would like to know how can I extend JEditorPane (or any other swing
I would like to know if there is a method for splitting a jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.