?
Solved

EASY - silly question from a beginner...

Posted on 2006-07-08
4
Medium Priority
?
221 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

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.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 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