I have no errors when public static void main(String[] args) is not in the program, but when I put it in everything becomes a mess.

package classexample;
public class ClassExample 
{
    public static void main(String[] args)
    {
    int Power = 0;
    int Speed = 0;
    int TraveledDistance = 0;

    void changePower(int newValue) 
    {
        Power = newValue;
    }
    void changeSpeed(int newValue)
    {
      Speed = newValue;  
    }
    void changeTraveledDistance(int newValue)
    {
      TraveledDistance = Speed + Power;
    }
    void printStates() 
    {
     System.out.println("Power: " + Power + "Speed: " + Speed + "Traveled Distance: " + TraveledDistance);
    }
    }
}

You are trying to declear methods inside the main method. After you declare what you want to have in the main, make sure you close it }. Also its a good practice to always declear your methods public/private instead of leaving them with a default public one

HI,
not to be a meanie, but the first problem you encounter is with the naming of your class.
The standard practice in coding is to use camel casing ClassExample would be more appropriate. what IDE are you using I would suggest using netbeans it's free. you are using void as an opening for your methods even in any class it is not acceptable. Again not trying to be mean. In not a not main class you would have to establish if the method was private or public. But in your main class there is no need. If you wish more help email me @ cdstrayer@yahoo.com

commented: Almost entirely nonsense or wrong -3

hi!

Nested Method is not allowing in Java..,

copy and past other methods out side the main method life time..,

Have A Happy Day..,

the first problem you encounter is with the naming of your class.

Hmm... I am trying to figure out about naming issue here (ClassExample)... The OP has the exact name so what is the problem???

cd88's post is, regrettably, partly nonsense and otherwize mostly wrong. His previous posts are about VB, so Java obviously isn't his area of expertise. Best simply to ignore that entire post.

Sorry guys, I'm still quite new to Java and english is not my mother language, so can you tell me step by step what to do? Or give me an edited version of my program and tell me what I did wrong, but simplified.

Simply:
You did this:

public static void main(String[] args) {
     ...
     void changePower(int newValue) {
          Power = newValue;
     }
     etc
}

You cannot create a method inside another method. You must finish each method before you start another, like this:

public static void main(String[] args) {
     ...
}

void changePower(int newValue) {
     Power = newValue;
}
etc

Hi,
I think you should declare your variable outside of the main() since it looks like they are the global variables.
In this case, your main should just call the different methods changePower(), changeSpeed() etc,
then you write your actual methods outside of main()and they have to be "static"
try to make the display nicer by adding spaces.
it then looks like something like this,

public class ClassExample {

    /**
     * @param args
     */
    static int Power = 0;
    static int Speed = 0;
    static int TraveledDistance = 0;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        changePower(67) ;
        changeSpeed(22);
        changeTraveledDistance(5);

        printStates();

    }
        static void changePower(int newValue) 
        {
            Power = newValue;
        }
        static void changeSpeed(int newValue)
        {
          Speed = newValue;  
        }
        static void changeTraveledDistance(int newValue)
        {
          TraveledDistance = Speed + Power;
        }
        static void printStates() 
        {
         System.out.println("Power: " + Power + "Speed: " + Speed + "Traveled Distance: " + TraveledDistance);}


}

Legal Java yes, but terrible advice for someone learning Java. Making everything static is missing the whole point of an object oriented language.

Much better to remove every static keyword except the one on main, and start main by creating an instance of the class with which to call the methods.

Those variables that you think look like "globals" should actually be instance variables.

And if you want to talk about making the display nicer...
indent correctly
keep comments with the code they refer to
remove obsolete comments
insert blank lines beween major code units rather than inside them
use a consistent style for brackets
indent correctly (it's worth saying this twice)
and
always indent correctly

"look like global variables"
even though static variables might look similar to global variables, there are no such things as global variables in Java.

I am guessing the word "global" means to its own class, not to the whole program where there are multiple classes being used???

That's the problem. Java doesn't have anything defined in the language spec. as "global", so you can use the word to mean anything you like. Similarly, your reader can read it to mean anything they like. Personally, if I were to use it, I wouldn't use it for anything less than a public static member or a public class.

You've been of great help guys, but my program still has one mistake.

    package classexample;
    public class ClassExample
    {
    public static void main(String[] args)
    {
    int Power = 0;
    int Speed = 0;
    int TraveledDistance = 0;
    }
    void Power(int newValue)
    {
    Power = newValue;
    }
    void changeSpeed(int newValue)
    {
    Speed = newValue;
    }
    void changeTraveledDistance(int newValue)
    {
    TraveledDistance = Speed + Power;
    }
    void printStates()
    {
    System.out.println("Power: " + Power + "Speed: " + Speed + "Traveled Distance: " + TraveledDistance);
    }
    }

At the second row I get an error "Duplicate class: classexample.ClassExample"

do you have another class with the same name in the package? that could explain this.

also, it is recommended to follow naming conventions, it 'll make it easier for other people that read your code to understand what is a class, what is a variable, what is a constructor and what a method.

No, the closest thing to it is another project called differentclasses.DifferentClasses. And sorry if it's messy, I'm still new to Java and I'm not sure where to write what.

You may have an old ClassExample.class file around somewhere from a previous compile. If so, search for and delete any files with that name.

Sorry about the delay. Yeah, I had some old projects lurking around that caused the problem. I thought I had deleted them but I guess they die hard. Anyway, thanks for the help mates!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.