Link to home
Start Free TrialLog in
Avatar of Wibble_
Wibble_

asked on

EASY - silly question from a beginner...

////////////////////////////////////////////////////
public class welcome {

      /**
       * @param args
       */
      public static void main(String[] args) {
            String strOne = "this is a string";
            
            int intOne = NumberOfBytes(strOne);
            System.out.println(strOne);
            System.out.println(intOne);
      }

}
class NumberOfBytes{
      int storage (String s){
            return s.length() * 2;
      }
}

////////////////////////////////////////////////////////
says:
the method NumberOfBytes(String) is undefined for the method welcome

why?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Change

>>class NumberOfBytes{

to

static class NumberOfBytes{

and

>>int intOne = NumberOfBytes(strOne);

to

int intOne = NumberOfBytes.storage(strOne);
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wibble_
Wibble_

ASKER

////////////////////////////////////////////////////
public class welcome {

     /**
      * @param args
      */
     public static void main(String[] args) {
          String strOne = "this is a string";
         
          int intOne = NumberOfBytes.storage(strOne);
          System.out.println(strOne);
          System.out.println(intOne);
     }


static class NumberOfBytes{
     static int storage (String s){
          return s.length() * 2;
     }
}
}
/////////////////////////////////////////////


Hurrah! Thank you CEHJ. It needs to be static, because otherwise it doesn't exist...

yay.
:-)