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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:10:52+00:00 2026-05-30T08:10:52+00:00

I have this class that everytime I call the constructor should increment the id

  • 0

I have this class that everytime I call the constructor should increment the id

public class Candidato {
    private String nome;
    private String cognome;
    private final int id;                 // id that tracks number of objects
    private static int counter=0;         //counter that increments the id
    private static int counterd=0;
    private static int counters=0;
    private static int counterp=0;
    private double voti;
    private int doc;
    private int stud;
    private int pta;

    public Candidato(String n,String c){
        this.id=counter++;           //everytime i call the constructor increment me
        this.nome=n;
        this.cognome=c;
        System.out.println(counter+"- -");  // here 
    }
          public int getNumero(){
        //System.out.println(id+"--id--");
            return id+1;
          }
}  

So my doubts arrise here : Suppose I got a class Elections that calls the Candidato constructor, how can i keep track of the Candidato id everytime a new instance of elections gets created ?

Like in this example at testNumeriCandiati() i get some strange numbers as they are not the expected ones, therefore everytime I create an Election class I want to set the Candidato id back to 0

    public class TestR2_Candidati extends TestCase {

        public void testCandidato(){
            Elezione sistema = new Elezione();

            Candidato c = sistema.nuovoCandidato("Marco", "Gilli");      
            assertNotNull("metodo nuovoCandidato() non implemenato",c);
            assertEquals("Marco",c.getNome());
            assertEquals("Gilli",c.getCognome());
            assertEquals(1,c.getNumero());
        }

    public void testGetCandidato(){
        Elezione sistema = new Elezione();

        Candidato c = sistema.nuovoCandidato("Marco", "Gilli");

        Candidato cc = sistema.getCandidato(1);

        assertNotNull("metodo getCandidato() non implemenato",cc);

        assertSame("Non viene restituito lo stesso candidato",c,cc);
    }

    public void testNumeriCandiati(){
        Elezione sistema = new Elezione();

        Candidato c1 = sistema.nuovoCandidato("Marco", "Gilli");
        Candidato c2 = sistema.nuovoCandidato("Francesco", "Profumo");
        Candidato c3 = sistema.nuovoCandidato("Rodolfo", "Zich");

        assertEquals("Non corrisponde il numero",1,c1.getNumero());
        assertEquals("Non corrisponde il numero",2,c2.getNumero());
        assertEquals("Non corrisponde il numero",3,c3.getNumero());
    }
}
  • 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-30T08:10:53+00:00Added an answer on May 30, 2026 at 8:10 am

    Your counter is stored as a static variable of Candidato, so its value does not depend on creation of instances of Elezione. If you want this funcionality you need to manage the counter of Candidato into Elezione:

    class Elezione {
        private candidatoCounter = 0;
        ................
    
        Candidato createCandidato() {
            return new Candidato(candidatoCounter++);
        }
    }
    
    class Candidato {
        private int id;
        Candidato(int id) {
            this.id = id;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a class that looks like this: class Sample { public string
I have this class that have a function to load other classes and create
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have this code inside a class that is used by an application and
I have this small class called City that simply holds some information about a
I have a class that contains data from some model. This class has metadata
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I have a generic class that takes a type T . Within this class

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.