Solved

EASY - silly question from a beginner...

Posted on 2006-07-08
4
206 Views
Last Modified: 2010-03-31
////////////////////////////////////////////////////
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
Comment
Question by:Wibble_
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17064816
Change

>>class NumberOfBytes{

to

static class NumberOfBytes{

and

>>int intOne = NumberOfBytes(strOne);

to

int intOne = NumberOfBytes.storage(strOne);
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17064825
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
 

Author Comment

by:Wibble_
ID: 17064909
////////////////////////////////////////////////////
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17065174
:-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 50
Java Restore security prompts not working 10 63
jmss example java 2 23
junit as external jar or library 7 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question