Results 1 to 4 of 4
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Error in a small code

    Can somebody tell me why am I getting this error. value IS a field of r2


    Java Code:
    package interfaceAsAType;
    
    public interface Runnable {
     int sum(Runnable run);
    }
    public class Thread implements Runnable{
     int value;
     int val;
     Thread(int a){
     value=a; 
     }
     Thread(int a, int b){
      value=a;
      val=b;
     }
    
    
     public int sum(Runnable r2) { 
      return this.value+ r2.value; //*********************************VALUE CAN NOT BE RESOLVED OR IS NOT A FIELD
     }
    
    
    }
    public class Main {
     public static void main(String[] args) {
      Runnable r1;
      r1= new Thread(2);
      Runnable r2;
      r2= new Thread(2,3);
      
      System.out.println("The sum of 2 and 2 is :" + r1.sum(r2));
    
    
     }
    
    
    }
    Don't forget to smile :-)

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Error in a small code

    r2 is of type Runnable, the data field 'value' is unknown to it.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Error in a small code

    It's not a valid field of the interface Runnable. In fact, interfaces can't have fields. It is a valid field of the class Thread.
    When you assign an instance of a class to an implemented reference type, only the methods of that type are available.
    The same is true when a sub class instance is assigned to a super class type that it inherits. And please, next time, don't confuse what
    you are trying to demonstrate by using two names of related JDK classes that have nothing whatsoever to do with actual threads.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Error in a small code

    Thank you!
    Don't forget to smile :-)

Similar Threads

  1. small piece of code: cannot set a max
    By senca in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 08:26 PM
  2. small code, need help
    By p0rnstar in forum New To Java
    Replies: 22
    Last Post: 01-22-2010, 01:57 PM
  3. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  4. Urgent small code
    By karingulanagaraj in forum New To Java
    Replies: 7
    Last Post: 08-11-2008, 04:11 AM
  5. small error
    By ayoood in forum New To Java
    Replies: 23
    Last Post: 05-27-2008, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •