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

  • Home
  • SEARCH
  • 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 8990679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:33:09+00:00 2026-06-15T22:33:09+00:00

My question is how can I write setter/getter methods and static fields in an

  • 0

My question is how can I write setter/getter methods and static fields in an interface and implement it in another class.

An example:

public interface MyInterface {
int number = 0;

public int setNumber(int num);{

 }
}

// Use it

public MyClass implements MyInterface{

 ...

public int setNumber(int num) {
   number = num;   // Error, Why?
 }
}

I get error on number = num but it get no error in the setName(...) method!

  • 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-15T22:33:10+00:00Added an answer on June 15, 2026 at 10:33 pm

    You cannot define instance fields in interfaces (unless they are constant – static final – values, thanks to Jon), since they’re part of the implementation only. Thus, only the getter and setter are in the interface, whereas the field comes up in the implementation.

    And setNumber should return a void instead of int. For getting I suggest you to add int getNumber().

    public interface MyInterface {
      void setNumber(int num); // public is implicit in interfaces
      int getNumber();         // obviously
    }
    
    public class MyClass implements MyInterface {
      private int number = 0;
      
      public void setNumber(int num) { this.number = num; }
      public int getNumber() { return this.number; }
    }
    

    As you can see, only setNumber is part of MyInterface. Consumers do not need to know about how the number is stored, therefore it is an implementation detail.

    Besides, in Java you name classes and interfaces in PascalCase rather than camelCase.

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

Sidebar

Related Questions

So I have a question on setter and getter methods and how useful they
i am using android:imeOptions=actionSearch in editText and my question is can i write my
Just a quick question. I can write char string[] = Hello world; char *p
My question is pretty simple: How can I write a function once and make
My question is not clear at title [i can not write it exactly] e.g
Here's a really simple class: static public class Bean1 { final private String name;
One hour ago I asked a question: how can I write an extension method
In a class, you can create fields within them, as int bar in the
For example I have SomeClass class which implements ISomeInterface interface with two properties Prop1
A nice C interview question: Can you write a function which swaps two int*

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.