• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comibining classes for one program  RSS feed

 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a VERY basic game but the combat part gets pretty complex so I want to do that part in a different class.

I understand this is a basic concept but I can't find the corresponding link in the Ranch, would be nice if someone sent mt the link to that or to some other site that explains it well.
I want to be able to import (or whatever else the correct term is) methods of that class into the main class, to make things less complicated. Plus, it's compulsory for my project.

Thanks in advance!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't import methods unless they are static.
It is a standard part of OO programming to use methods called on a reference to an object of another class. Do you not know how to do that?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do import your classes, but this just tells Java where to find the class.

Then you use the new keyword to create instance of the class (an object, or technically, a reference to it).

Then you use this reference to a object to invoke the methods of the object.

Google "java new" or "java creating objects" and then port back here what you've done.
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing I have done relating to objects is creating constructors, but everything so far has been within one class. I have imported packages like io, but never linked two classes together. There's a reason I posted this in "Beginning Java" you know. Will Google for stuff.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't mean to be flippant. I just wanted you to look into subject that is a little too broad to post here. I'll give you an example and if you have specific questions I (or someone) will answer them.

Say you have a class Circle. It would go in a file called Circle.java. It might look like this:



Then you might create a Circle object like this:



Now there's a ton of information I'm leaving out, but that's why I suggested you look into it, so you could ask a specific question.

Good Luck!
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a good idea now. Could you give me a link to a simply-written article where one could read in depth about this particular topic? The Google results are quite overwhelming...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html

Since import is also tied down to packaging your classes I would suggest you start reading from http://docs.oracle.com/javase/tutorial/java/package/index.html
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, got some reading to do now. This thread may be closed, ADMIN.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!