Results 1 to 7 of 7
  1. #1
    Reptar is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Total noob here, i'm getting an error

    I'm trying to run the code below in Eclipse and I'm getting the error message "Selection does not contain an applet" if I run it as an applet, and "Selection does not contain a main type" if I run it as an application. How do I fix this? I'm entirely new to java (only've done PHP and javascript, if those even count), so extra detail is more than welcome.

    Code:
    Java Code:
    package svidler.net;
    
    public class Cube {
    	//fields
    	int length;
    	int breadth;
    	int height;
    	public int getVolume(){
    		return (length * breadth * height);
    	}
    }
    
    cubeObject.length = 4;
    cubeObject.breadth = 4;
    cubeObject.height = 4;
    cubeObject.getvolume();
    Screenshots:


  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You're trying to call code outside of a method or constructor, heck it's even completely outside of the class. Programs must be constructed in a structured way, and before you can start writing programs, you must understand this structure -- there unfortunately are no shortcuts. Please re-review your notes and texts to see how to construct a simple Java program. Surely these references will have a sample program that you can use as a basis for your program.

    Que tenga suerte!

  3. #3
    Reptar is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    I took a look at some example codes and I think I understand the structure more now. I changed the code to:

    Java Code:
    package svidler.net;
    
    
    public class Cube {
    	//fields
    	int length;
    	int breadth;
    	int height;
    	public int getVolume(){
    		return (length * breadth * height);
    	}
    	public static void main(String args[]) {
    		Cube.length = 4;
    		Cube.breadth = 4;
    		Cube.height = 4;
    		Cube.getvolume();
    	}
    }
    Everything seems to be fine except for where I have "Cube.length=4; Cube.breadth = 4; ...." And I get a message, "Description Resource Path Location Type
    Cannot make a static reference to the non-static field Cube.length Cube.java /First Project/src/svidler/net line 13 Java Problem
    ", for all 4 of those lines (13, 14, 15, 16).

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You're getting closer as now you have code enclosed within methods, but you'll need to learn what an object is and how to create them. The main method is a static method and while it is part of your Cube class, it is not a method of of a Cube object, and all non-static variables and methods of Cube are not directly accessable by main or any other static method.

    What you want to do instead is to create a Cube object within the main method, and then set the fields (also known as the variables) of this object.

    For example:

    Java Code:
    public class MyClass {
       //fields
       int value;
       public int myMethod() {
          return value;
       }
       public static void main(String args[]) {
          // first create an object of the class:
          MyClass myObject = new MyClass();
    
          // then you can access the object's fields
          myObject.value = 3;
    
          // and its methods:
          System.out.println("value = " + myObject.myMethod());
       }
    }
    Later you'll not want to fiddle with an object's fields directly but rather in very controlled ways such ais via constructor parameters.

  5. #5
    Reptar is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    I tried doing this:
    Java Code:
    package svidler.net;
    
    
    public class Cube {
    	//fields
    	int length;
    	int breadth;
    	int height;
    	public int getVolume(){
    		return (length * breadth * height);
    	}
    	public static void main(String args[]) {
    		Cube mycube = new Cube();
    		mycube.length = 4;
    		mycube.breadth = 4;
    		mycube.height = 4;
    		mycube.getVolume();
    	}
    }
    how would I know if it works?

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    This method:
    Java Code:
    mycube.getVolume();
    returns an int. If this were my program, I'd print out the int using System.out.println(...) to see if the results match my expectations.

  7. #7
    Reptar is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. A total beginners problem
    By mbezel in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 10:07 PM
  2. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 03:10 AM
  3. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  4. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM
  5. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •