SRL Archive - Java Help and Tutorials

Java Help - Objects and Multiple Classes


Sax

2013-09-01 17:36:12

So, I am having a bad time here. I watched the newboston video tutorials on objects and multiple classes and now I'm trying to do myself just to see if I got it.


My classes:

package bucky;



public class bacon {
private String sport;

public void setSport(String sportname) {
sport=sportname;
}


public String getSport() {
return sport;
}

public void SaySport() {
System.out.printf("Really?? %S is my favourite sport too!", getSport());


}

}


package bucky;


import java.util.Scanner;

public class banana {

public void main(){
Scanner choco= new Scanner(System.in);
System.out.println("What is your favourite sport?");
String temp = choco.nextLine();
bacon object = new bacon();
object.setSport(temp);
object.SaySport();
}

}


And this is the error I got:

Error: Could not find or load main class bucky.apples



I happen to have a class named apples which is the class that I copied from the tutorial but I don't see how I get this error.
Anyone?

Thanks everybody in advance :D

Brandon

2013-09-01 19:28:51

should be:


package bucky;

public class apples {
public static main(String Args[]) {
}
}

Sax

2013-09-03 12:19:40

But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

Thanks again for your help Brandon :D

J J

2013-09-03 12:24:25

From: Nirvana
But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

Thanks again for your help Brandon :D

It happens somewhere in the mainloop. I think Bucky's way of naming the classes is extremely confusing.

It would have been better to have

House
-> University extends House
Person
-> Student extends Person
-> Teacher extends Person
-> -> Professor extends Teacher

Then have an Object Subject
Subject(String name)

And a Student has a Teacher which gives a Subject, etc. but that stuff is more for future tutorials. But other names would've been better, they confused me aswell.

Brandon

2013-09-03 20:55:20

From: Nirvana
But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

Thanks again for your help Brandon :D



I did not say that the bacon class should be renamed (I didn't have time to elaborate so I thought you'd get the hint). Look at the left-side of your picture. It says "apples" is a class in the package "bucky".
Your apples class is missing the function:


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

That is what your error is saying. It says that apples does not contain a main and that it cannot find main within bucky.apples. You must have told it that apples would be the main class when you created the project. Hopefully I didn't reply too late but yeah.

Sax

2013-09-04 08:23:00

From: Brandon
I did not say that the bacon class should be renamed (I didn't have time to elaborate so I thought you'd get the hint). Look at the left-side of your picture. It says "apples" is a class in the package "bucky".
Your apples class is missing the function:


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

That is what your error is saying. It says that apples does not contain a main and that it cannot find main within bucky.apples. You must have told it that apples would be the main class when you created the project. Hopefully I didn't reply too late but yeah.


Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

Thank you for your patience. :)

masterBB

2013-09-04 08:59:52

Some project option. I have no experience with that ide. But usually it is right clicking on the class in the class tree and sellecting 'main class' or something like that.

Daniel

2013-09-04 09:50:37

From: Nirvana
Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

Thank you for your patience. :)


There should only be one public static void main(Stringp[] args) in your project. This method is the "main event loop", i.e. the very first method that is called when you run your program.

It must have a main event loop. The class, SimpleCalculator, seems like an appropriate place to put this main method. Try adding it there.

From: masterBB
I have no experience with that ide.


It's Eclipse.

Brandon

2013-09-04 09:50:41

From: Nirvana
Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

Thank you for your patience. :)



Just add the main to the banana class. Then right click your banana class and click properties then "Run/Debug Settings" on the side. Set the main class as bucky.bacon. Save and exit.
Delete the other two classes that you don't want.
Select the banana class then go to the run menu and run-as Java-Application