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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:08:32+00:00 2026-05-17T19:08:32+00:00

public class ElapsedTime { public int hours; public int minutes; public void ElapsedTime(int h,

  • 0
public class ElapsedTime
{
    public int hours;
    public int minutes;
    public void ElapsedTime(int h, int m)
    {
        hours = h;
        minutes = m;
    }
}

from another event i am doing this:

    ElapsedTime BeginningTime = new ElapsedTime();

how would i initialize the h and m?

when i try to do this: BeginningTime.ElapsedTime(7, 7);

it gives me this error:

Error 1 ‘ElapsedTime’: member names
cannot be the same as their enclosing
type

all i want is a class with a constructor that accepts initializing values. and i want to be able to call it.

UPDATE:

now i have :

 public class ElapsedTime
        {
            private int hours;
            private int minutes;
            public ElapsedTime(int h, int m)
            {
                hours = h;
                minutes = m;
            }
        }

its giving me that same erorr on public ElapsedTime(int h, int m)

  • 1 1 Answer
  • 1 View
  • 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-17T19:08:33+00:00Added an answer on May 17, 2026 at 7:08 pm

    The constructor of a class needs to be of the same name of the class itself. Now, the parameters h and m make it a parameterized constructor. In order to initialize a instance of this class, you will need to specify values to those.

    ElapsedTime beginningTime = new ElapsedTime(7, 7);
    

    Calling the constructor through a variable is not doable:

    ElapsedTime beginningTime;
    beginningTime.ElapsedTime(7, 7); // This will cause an error, as your constructor isn't a method...
    

    In inroder to be a class initializer, the constructor has no return type (neither void).

    Of course, if you want a property which gives you the values under a certain format, you will need to expose a property which will provide you with the values you instantiated your instance:

    public class ElapsedTime {
        public ElapsedTime(int h, int m) {
            Hours = h;
            Minutes = m;
        }
    
        public int Hours { get; private set; }
        public int Minutes { get; private set; }
        public string ElapsedTime {
            get {
                return string.Format("{0}:{1}", Hours, Minutes);
            }
        }
    }
    

    Did I understand your question correctly?

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

Sidebar

Related Questions

public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class Test{ public static void main(String[] args){ int[] a = {3, 2, 5,
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class TextFileExtractor{ public static String[] fileExtractor(String[] s){ StringBuilder sb=new StringBuilder(); for(int i=0;i<=s.length;i++){ if(s[i].endsWith(.txt)){
public class TextFileExtractor{ public static String[] fileExtractor(String[] s){ StringBuilder sb=new StringBuilder(); for(int i=0;i<=s.length-1;i++){ if(s[i].endsWith(.txt)){
public class MyClass { public string MyProperty{ get; set; } Now, I would like

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.