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 8167093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:05:41+00:00 2026-06-06T20:05:41+00:00

OK guys, up till now (and since I’m a beginner) I was programming Java

  • 0

OK guys, up till now (and since I’m a beginner) I was programming Java based on procedural programming and it was nice and all but It’s time to use Java like-a-boss.

I’m learning the OOP concept now while writing some code as practice.
What I dont understand is that if I create a few objects this way:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

The result will be:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam

But if I then try to print the first contact again, it will get the values of the last object created. I mean, for this code:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

the result will be:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam
Age of contact Contact@173f7175 is - 34 Adam

I’ve added up the object string representation so you can see the different objects.
I thought I was creating a new object and each object has it’s own instance values?
could you guys explain to me?

This is the contacts class:

public class Contact {

    private static int age = 0;
    private static String name = "Unknown";
    private static String gender = "Male";

    public Contact(int a, String n, String g) {
        age = a;
        name = n;
        gender = g;

    }

    public Contact() {
    }

    public static int getAge() {

        return age;
    }

    public static String getName() {

        return name;
    }

    public static String getGender() {

        return gender;
    }

    public static void setAge(int a) {

        age = a;

    }

    public static void setName(String n) {

        name = n;
    }

    public static void setGender(String g) {

        gender = g;
    }

}

Please note that if I remove static qualifier I get errors saying “cannot make a static referance to the non static field”

  • 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-06-06T20:05:44+00:00Added an answer on June 6, 2026 at 8:05 pm

    Remove the static qualifier from your instance variables and/or methods (age, getAge, name, getName).

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

Sidebar

Related Questions

I've developed some web-based applications till now using PHP, Python and Java. But some
Guys. I use getpixel & getdata to retrieve data from the same pic.but some
Hi guys, I am developing an application for both tablets as well as mobile,Till
Guys i able to play audio file using Mediaplayer. But i need to play
Guys!! I'm really enthusiastic in learning django and learnt a bit on Django-1.2 but
Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
Guys, this probably will be fairly simple, but how am I able to find
Guys can you give a good comparison regarding Oracle/PL SQL and Java in terms
guys, I am trying to make use of glVertexAttribDivisor in my OpenGL instanced drawing.
guys, i want to click my uiButton after it's added to UIImageVIew, but it

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.