Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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?
0
Wibble_
Asked:
Wibble_
  • 3
1 Solution
 
CEHJCommented:
Change

>>class NumberOfBytes{

to

static class NumberOfBytes{

and

>>int intOne = NumberOfBytes(strOne);

to

int intOne = NumberOfBytes.storage(strOne);
0
 
CEHJCommented:
Two more things:

1. move your second '}' right to the end of the file
2. change

>>int storage (String s){

to

static int storage (String s){
0
 
Wibble_Author Commented:
////////////////////////////////////////////////////
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.
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now