Results 1 to 4 of 4
Thread: methods and packages
- 02-08-2010, 11:19 AM #1
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
methods and packages
hello,
i know this may be a little noob question but anyways i can't sort it out.
i'm making a little rpg game...
i wanted to make a method buyClothes wich the user can purchase clothes.
because the method has to be public(the main needs to acces it)
i couldnt make it in that file. because when i tried to make a public method it gives me illegal start of type error.
so i decided to make a new class file in this package...
and put my method buyClothes in there.
now how can i acces my method in the main file?
because when i type
it says it can't find method... how to acces a method from another file?Java Code:buyClothes();
Beginner in Java Programming, Please don't trust my anwsers blind please :D
- 02-08-2010, 11:30 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Accessing methods from other classes is done in 2 ways, depending on the method, if it's static or non-static.
Static methods are accessed by typing the name of the class in front of the method:
Non-static methods can only be accessed through an object-instance of the class:Java Code:MyClass.myMethod();
Java Code:MyClass a = new MyClass(); a.myMethod();
- 02-08-2010, 11:38 AM #3
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
thanks a lot
Beginner in Java Programming, Please don't trust my anwsers blind please :D
- 02-10-2010, 09:33 AM #4
hi coltragon,
i guess below code can help you, good luck
Java Code:public class AccessOneMethod { public static void main(String[] args) { Person p = new Person(); p.buyClothes(); } } class Person{ public void buyClothes(){ } }i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Packages
By urbim in forum EclipseReplies: 2Last Post: 07-02-2009, 02:01 PM -
Importing packages from the packages within same application.
By sta2003 in forum New To JavaReplies: 3Last Post: 02-12-2008, 11:03 AM -
Using packages
By prfalco in forum New To JavaReplies: 5Last Post: 01-31-2008, 10:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks