How do you make a static reference to a method?  I've included code.

I'm sorry but this is a cross post. This should be here but it is also in the 100% pure Java forum. It won't happen again.
Now...
Why doesn't this work? How do I use the method add(int a, int b)?
ERROR - "Can't make static reference to method int add(int, int) in testClass"
interface testInterface{   
    static String sString = "TESTING";   
    public int add(int a, int b);
class testClass implements testInterface{
    public int add(int a, int b){
        return a+b;      
    public static void main(String argv[]){
        int sum = add(3,4);    // here's the error
        System.out.println("test");
        System.out.println( sum );   
}Again, I apologize for the cross post.

hi,
this seems to be pretty easy, isn't it?
Oh c'mon! You try to invoke a non-static method from within a static method. Solution: Create a specific instance of class testClass:
testClass test=new testClass();
test.add(3,4);best regards, Michael

Similar Messages

  • How do I make a static reference to a method?  Sample Code Included

    Why doesn't this work? How do I use the method add(int a, int b)?
    ERROR - "Can't make static reference to method int add(int, int) in testClass"
    interface testInterface{
        static String sString = "TESTING";
        int add(int a, int b);
    class testClass implements testInterface{
        public int add(int a, int b){
            return a+b;  
        public static void main(String argv[]){
            int sum = add(3,4);    // here's the error
            System.out.println("test");
            System.out.println( sum );
    }

    Why doesn't this work?Because you can't call a non-static method like add (which operates on the object called this) from a static method like main (for which there is no this).
    There are two ways to fix this:
    (1) In main, create a TestClass object, and call add() for that object:
    class TestClass implements TestInterface {
       public int add(int a, int b) {
          return a+b;
       public static void main(String[] args) {
          TestClass testObject = new TestClass();
          int sum = testObject.add(3, 4);
          System.out.println("test");
          System.out.println(sum);
    }(2) Make add() static. This is the preferred approach, because add() doesn't really need a TestClass object.
    class TestClass implements TestInterface {
       public static int add(int a, int b) {
          return a+b;
       // main is same as the original
    }

  • How do you make an array of image icons and then call them?

    How do you make an array of image icons and then call them, i have searched all over the internet for making an array of icons, but i have
    found nothing. Below is my attempt at making an array of icons, but i cant seem to make it work. Basically, i want the image to match the value of the roll of the dice (rollVal)
    Any help would be greatly appreciated, some code or link to tuturial, ect.
    /** DiceRoller.java
    * Roll, print, Gui
    import javax.swing.*;
    public class DiceRoller extends JFrame
         private ImageIcon[] image  ;
         public String[] images = { "empty", "dice1.jpg",
                   "dice2.jpg", "dice3.jpg", "dice4.jpg",
                   "dice5.jpg", "dice6.jpg" };
         public Dice die;
         private int rollVal;
         public int rollNum;
         private JLabel j1;
         public DiceRoller(){
              j1= new JLabel("");
           die =new Dice();
           int rollVal;
           rollVal = die.roll();     
           image = new  ImageIcon[images.length];
         for(int i = 0; i < images.length; i++){
          image[i] = new ImageIcon(images);
         if (image!=null){
              j1.setIcon(image[rollVal]);
         System.out.println("Roll = "+die.roll());

    Demo:
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    public class IconExample {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    launch();
        static void launch() {
            try {
                Icon[] icons = new Icon[6];
                for(int i=0; i<icons.length; ++i) {
                    String url = "http://www.eureka-puzzle.be/cast/images/dice" + (i + 1) + ".jpg";
                    icons[i] = new ImageIcon(new URL(url));
                display(icons);
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
        static void display(Icon[] icons) {
            JPanel cp = new JPanel();
            for(Icon icon : icons) {
                cp.add(new JLabel(icon));
            JFrame f = new JFrame();
            f.setContentPane(cp);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • Newbie Question...  How do you make sure java can find all the imports

    Hi everyone,
    I am new to Java and so far I really like it. But I am having problems porting applets to my websites. How can you make sure that all the references can and will be found in the applet...
    for example,
    I import javax.media.ControllListener in a project I am using and it works great in the applet viewer but when I try to port this to my website I'm working on, java can't find it. I really would appreciate any help on this cause then I would have java power :) and will keep me from losing my hair :).
    Thanks for those who help me.
    - Stan

    Hey jwenting,
    Thanks for the quick response. Classpath definitely makes sense so I have been reading up on that... But do you include .classpath file in your website too? And how would you reference it if you need to reference it?
    - Stan

  • Error: Cannot make a static reference to the non-static method

    Below is a java code. I attempt to call method1 and method2 and I got this error:
    Cannot make a static reference to the non-static method but If I add the keyword static in front of my method1 and method2 then I would be freely call the methods with no error!
    Anyone has an idea about such error? and I wrote all code in the same one file.
    public class Lab1 {
         public static void main(String[] args) {
              method1(); //error
         public  void method1 ()
         public  void method2 ()
    }

    See the Search Forums at the left of the screen?
    If you had searched with "Cannot make a static reference to the non-static method"
    http://search.sun.com/search/onesearch/index.jsp?qt=Cannot+make+a+static+reference+to+the+non-static+method+&rfsubcat=siteforumid%3Ajava54&col=developer-forums
    you would have the answer. Almost every question you will ask has already been asked and answered.

  • How do you make a web browser?

    I would like to make a Web Browser of my own but how did you make FireFox?

    Read this:
    http://thepattysite.com/window_sizes1.cfm
    You don't want to try to launch your site to full screen,
    honest, as you'd
    drive your (potential) visitors away.
    Jo
    "Steve Zavodny" <[email protected]> wrote in
    message
    news:escrfa$s7$[email protected]..
    > How do you make a web browser launch your site to a
    specific size or even
    > full screen?
    > Thanks...

  • How do you make superscripts and mathematics in Preview's text annotation?

    When adding text annotations that include scientific notations, how do you make superscripts, e.g. "3.2 × 10^{-23}"?
    Searching Help for 'superscript' turns up nothing, suggesting the command does not exist, as it does under Baseline etc. in iWork. Please tell me this is not the case! If it is, has the situation been improved in the new operating system?
    A related issue: I wish to include other mathematics -- is there a way to include mathematics similar to LaTeX? Might Preview incorporate using latex code in the future, so I can easily type "\exp{-\lambda t}" etc and have the math appear?
    I'm adding text annotations to PDF lecture notes in Preview, basically filling in blanks the instructor has left so students will pay attention in class, and these annotations include superscripts; I wish to format properly.
    Thank you!

    Once you have the circle shape displayed, long press on the border of the circle. Once it is highlighted, drag horizontally or vertically. This should allow you to distort the circle into an ellipse (or the square into a rectangle).
    Hope that helps,
    Frank
    Sketch Engineering

  • How can you make one button preform different actions in Xcode?

    How can you make one button "preform a click" and "orderfront"? Is that possible? Because what I am doing is making a welcome window to my application for Mac OS X 10.6, and I have two buttons in that window, one says "Connect" which connects a a WebView to a url and the other button opens up the main window. So how can I a make a button do multiple things at once??

    Ah - If you are just using bindings between various objects from Interface Builder, you can reduce your application code a bit (sometimes quite a bit), but there is only so far you can go. Typically variables and routines are defined in your application that Interface Builder hooks into, so that your application code (usually Objective-C, but can be others) can use these variables and routines to determine what is happening in the user interface.
    I'm not sure how up-to-date the project code is (or what programming code you are familiar with), but you can take a look at Apple's Cocoa Application Tutorial to see what is (usually) involved in creating an application.

  • How can you make the iPod tell the diff between a movie and music video

    Greetings!
    How can you make iPod tell the difference between a Music Video and Movie? Everything I put up on the iPod falls into Movie. So is it something I have to do in iTunes?
    Thanks for the help!

    Rightclick the video in iTUnes, hit Get info then the "Options" tab. Beside kind you can set it to movie. music video. or TV show

  • How do you make the sentences you input in a drop down list wrap to the next line?

    How do you make the sentences that you input in a drop down list to wrap to the next line?

    You can't.
    see this thread for more info:  Can you set drop down list as multi-line???

  • HT204053 I have multiple devices used by family members, how do you make sure the kids don't see Moms messages from Dad and visa versa?

    I have multiple devices used by family members, how do you make sure the kids don't see Moms messages from Dad and visa versa?

    Hi Roger:
    Thanks for the reply.  I think I figured out the problem.  Apple has confused (at least in my mind) the different IDs that people can have.  In the iCloud settings, they use the term Apple ID and not iCloud ID.  I guess there really is no such thing as an iCloud ID.  There are Apple IDs with various attributes.  This is not clearly explained or easy to see (how could I look up to see if my Apple ID is associated with iTunes for example?).
    I created, what I thought were iCloud IDs for each family member, but we all use my Apple ID for iTunes.
    One other question - why does the syncing of Notes using iCloud require an @me.com email account.  That makes no sense to me.
    Thanks for your help!
    AO

  • How do you make a 3d octagon in photoshop cs4 extended?

    How do you make a 3d octagon in photoshop cs4 extended that you can map artwork to? I've been searched online for a solution that doesn't involve using a 3D program but can't find anything. Can't you extrude a shape like in Illustrator, or exporta true 3D shape from Illustrator that will work? I'd use Illustrator but the mapping thing is finicky, and doesn't alow wrapping images around angles. (now someones going to say: "Don't mention any other programs in a PHotoshop forum!" Yeah yeah, there should be a forum then about using different Adobe programs together.) End of rant, any suggestions much appreciated.
    Thanks,
    Mike

    Thanks for your prompt response!
    Suggested Bridge Update
    It turns out that I got a message that this update had already been installed, which led me to more carefully look at the Bridge window and (finally) see that the Output module had a Quality dropdown: Full, 300 ppi, 150 ppi, 96 ppi and 72 ppi.  Thanks!  I decided to compare the CS4/Bridge results to those I got in CS2 with its convenient File/Automate/PDF Presentation/Multi-Page Document routine that I like.
    My project was an 8-page PDF made from scanned pages of handwritten notes into which some fairly small type had been insert using the Photoshop CS2 Type tool.  In Photoshop CS2, I had settled on "medium quality" JPEG compression as giving a "reasonable" file size (about 7 MB) and "very good/excellent" image quality.  I experimented with making the PDF in Bridge, using all five Quality levels.  See table below.  The Bridge "good minus" file was about the same size as the file produced in CS2, which I judged to be "very good/excellent" and slightly better visually than the 300-ppi, 23.5-MB Bridge file.
    Bridge Quality:
    Full
    300 ppi
    150 ppi
    96 ppi
    72 ppi
    File Size:
    25.2 MB
    23.5 MB
    7.6 MB
    3.2 MB
    2.1 MB
    Subjective Quality:
    Very good
    Very good
    Good minus
    Poor
    Useless
    I appreciate that you may not be all that interested in these observations, but it seems to me that Adobe has done us no favors in the multi-page PDF department in going from CS2 to CS4!
    Install the Old Contact Sheet/Picture Package Plug-Ins
    I had already done this some time ago, in order to get in CS4 the File/Automate/Contact Sheet II feature I liked in CS2.  It does not appear to me that this plug-in has anything to do with make multi-page PDF's.  Perhaps I'm missing something.
    Again, thanks for your response!

  • How do you sync iCloud (contact, calendar, task) to outlook 2011 for Mac? and how do you make seemless sync between iCloud to Outlook 2011 to Blackberry device or an iPad?

    I have three device: Macbook Pro (OSX 10.7.5) for work; an iPad and a Blackberry for mobile. I used mail, iCal, and Address Book combine with mobileme (iCloud) to exchange data such as contact, calendar and task from my Mackbook Pro to my iPad but never to Blackberry. The problem is my boss was asking me to change to Outlook 2011 for Mac because some of the email that I sent from Mail did not appear in my client's Blackberry. So how do you sync iCloud (contact, calendar, task) to Outlook 2011 for Mac? and how do you make seemless sync between iCloud to Outlook 2011 to Blackberry device or an iPad?  
    Best regards,
    W. Sasongko

    More Like This --------------------------------------->

  • How do you make a MacBook Pro talk to an Epson printer?

    MacBook Pro
    OS X 10.6.8
    (Thinking about getting  another MacBook)
    I had an Epson Artisan printer, got an Epson Workforce WF-3640 new in Jan, hardly used because I cannot make it do what I want it to do. Wireless connection is good.
    With the MacBook Pro, the interface with the Epson printers is not what it used to be on the G4: no ColorSync, no 2-sided, no thick paper, none of those kinds of instructions appear on the generic printer control panel. Which by the way, bears NO resemblance to the Epson manual screenshots, and the instructions in the Epson manual are useless.
    How do you make a MacBook Pro talk to an Epson printer? And by the way, I cannot use the scanner either. I have to set up a document to print, then when the printing progress box appears, click on the scanner icon.
    Also
    Printer has 2 compartments. Top compartment #1 for all types paper.
    Bottom compartment #2 for plain paper only.
    I load glossy photo paper in #1, it prints plain paper from #2 even though I entered instructions on the control panel on the printer.

    If you haven't done so already, try resetting the printing system.
    Reset Printing System

  • How do you make the background of an object transparent in Adobe Illustrator?

    Hello Forum.
    How do you make the background of an object transparent in Adobe Illustrator?
    I have been having issues with using the Magic Wand tool in Adobe Illustrator as well as the lasso tool.
    Basically, I am trying to put a small graphic into a new Illustrator file.
    The first thing I tried to do is to use the magic wand tool.  But I found out that this did not work on a jpeg image since it did not have any individual objects to select.
    So I tried something new.  I opened the file in Adobe Photoshop and used the lasso tool to select an area.  Then I coped this area.  Then I tried to paste it into Adobe Illustrator.  But the background was not transparent for the pasted image.
    How do I make the background transparent.
    I made this small video to show what I am talking about:
    http://youtu.be/2LgMGEF6z10

    Don't Copy/Paste the image.
    Make it transparent in Photoshop, save a PSD and place it in Illustrator

Maybe you are looking for