Results 1 to 5 of 5

Thread: assigning a value inside interface....

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    assigning a value inside interface....

    I have the following code...

    Interface test
    {
    int i;
    }

    it shows the error when i compiled it. Then i assigned a value for i=0. After this correction it's working fine.

    My doubt is "Why we assign a value ?". Because in interface it is defaulty static and final. if i use a static field in my class then it is automatically assigned for "0". But here it shows error ? Why this error will come ?

    ----------------
    suresh


  2. #2
    Junior Member
    Join Date
    Feb 2007
    Answers
    23

    Re: assigning a value inside interface....

    In java, final variables are not assigned a default value. As you have pointed out, variables in interfaces are implicitly final.

    Hence, you have to assign value mandatorily.


  3. #3
    Junior Member
    Join Date
    Feb 2007
    Answers
    10

    Re: assigning a value inside interface....

    Hi suresh,
    In interface what ever values you will declare tht will ct as a final values. This values will be applicable for your entire application.

    Thanks,
    vishnu.


  4. #4
    bhagatrawat
    Guest

    Re: assigning a value inside interface....

    Hi,
    The answer is itself in your's question. You said that all the variables of an interface are automatically static and final. Every final variable need to be assigned some value in class or interface, otherwise it will through compilation error. Please follow the blow code :

    interface test
    {
    final static int i; // compilation error.
    }
    or
    interface test
    {
    static int i; // compilation error.
    }

    or

    class test
    {
    final static int i; // compilation error.
    }

    so it should be like this:

    interface test
    {
    final static int i = 3.49; // No compilation error.
    or
    int i = 3.49;
    }

    or

    class test
    {
    final static int i = 3.49; // No compilation error.
    }

    class test
    {
    static int i; // No compilation error.
    }

    Hope you will be agree with me.

    Thanks

    Bhagat Singh


    Quote Originally Posted by psuresh1982 View Post
    I have the following code...

    Interface test
    {
    int i;
    }

    it shows the error when i compiled it. Then i assigned a value for i=0. After this correction it's working fine.

    My doubt is "Why we assign a value ?". Because in interface it is defaulty static and final. if i use a static field in my class then it is automatically assigned for "0". But here it shows error ? Why this error will come ?

    ----------------
    suresh



  5. #5
    Junior Member
    Join Date
    Feb 2007
    Answers
    10

    Re: assigning a value inside interface....

    hi ,
    what ever variables you declare in interface that variable are static & final variables it will ask declaration that's why we need to provide variable value at the time of declaration.

    Thanks,
    vishnu.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact