Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Difference between a "Method" "Class" and "Object" (Java)?  (Read 54920 times)

AllThingsLive

  • Bay Watcher
  • Damn, I'm a sexy bitch!
    • View Profile
Difference between a "Method" "Class" and "Object" (Java)?
« on: September 03, 2011, 02:49:22 pm »

I've been reading Java concepts 6th edition for a while now, and we're getting into classes, methods and objects. The book goes over their definitions briefly, but I feel like I don't quite understand exactly what an object is, nor am I really able to tell the difference between an object  and a method or class. I understand what a class is and what a method is, and I understand THEIR differences, but I have no such luck with objects.
(Also, when making a Java program, don't you have to have a class named after the program? For instance, Minecraft.jar MUST have a minecraft.class.)
Logged
If you haven't already, you MUST listen to the Joe Rogan Experience : http://itunes.apple.com/us/podcast/the-joe-rogan-experience/id360084272

GlyphGryph

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #1 on: September 03, 2011, 02:59:21 pm »

A class is a set of rules you write that govern an object. You can have more than one class involved in creating an object through inheritance.

An object, or instance, or instance object, or class object, or whatever, is what a class defines. When you actually use that class to make a THING, this is what you make.

A method is a bit of code that can be called when needed (often as part of an class, but not always)


Here's an example: I write a class called "Ball"

In my main code, I then write "a=Ball.new, b=Ball.new, c=Ball.new". I still only have one class, "Ball", but I also have three objects (referenced by "a", "b", and "c"), whose creation was defined and handled by the class. I can then call the method "a.roll". This looks at "a", realizes it's an object of type "Ball", finds the method "roll" associated with "Ball", and executes it based on whatever attributes "a" has (weight, size, stickyness, whatever).

Not that you can have class-level methods as well. Most methods need a particular object to reference, but some (like "new" in "Ball.new") can be called based solely on the class.
« Last Edit: September 03, 2011, 03:03:35 pm by GlyphGryph »
Logged

freeformschooler

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #2 on: September 03, 2011, 03:28:22 pm »

Well, if you're familiar with any other programming languages or concepts, a method is often called a "function" in other languages. Anyway, GlyphGryph's explanation is pretty good.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #3 on: September 03, 2011, 03:31:31 pm »

An object, or instance, or instance object, or class object, or whatever, is what a class defines. When you actually use that class to make a THING, this is what you make.

An object is a chunk of memory that can be manipulated through various commands. Objects can be values, variables, functions, structs, unions, classes or whatever. Simply put, an object can be found to the left of an equals sign.

Code: [Select]
int a = 10; // "int" is a type, "a" is an object, and "10" is the value assigned to your object.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #4 on: September 03, 2011, 08:01:34 pm »

Nonsense, the thing on the left of the equal sign is a variable, a reference, nothing more! The object is the thing on the right, the equals sign is just telling the variable to point to it.

Okay, that's not always true, but what ILikePie said is just wrong. You can have many variable pointing to the same object. None of them ARE the object.

It's not always necessarily a single "chunk" of memory, either, though have some sort of something being stored somewhere is an important bit for them.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #5 on: September 03, 2011, 08:38:42 pm »

The point is that a class is like a set of blue prints for an object. (I'm going to pretend static doesn't exist yet, we can teach him that once he understands this) As such you can never have, or hold, or do anything with a class, you can just use it to build objects. An object is something you can use.
For example, a cat is a class. There are lots of cats, and although there are differences, they all follow the same design pattern. However, if you have a specific cat, that is an object. This one cat can be petted, it is real, it is not the blue prints but something that you can work with.

Think you get it? It is pretty much the same in all OOP languages, so if you get this for Java, the same rules ally across the board.

eerr

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #6 on: September 03, 2011, 10:48:54 pm »

You define methods in classes.

You make "objects" using a class as a template.

Some Classes organize methods.

Some Classes can be used as a template to make new objects.


When you run a Jar program, Java is loaded up, the classes are loaded into memory, and the program may create new objects that also get loaded into memory.

Minecraft.jar MUST have a minecraft.class?
Er, .jar is an archive type like .zip. You can make a .jar without any .java files, or class files.
Minecraft.jar doesn't haaave, to run minecraft.class
It does because thats the way Notch made it.
« Last Edit: September 03, 2011, 11:37:37 pm by eerr »
Logged

AllThingsLive

  • Bay Watcher
  • Damn, I'm a sexy bitch!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #7 on: September 04, 2011, 12:03:53 am »

@MaxWhite
The concept of an object in Java is right "On the tip of my tongue" in terms of conceiving of it. I'm so close to understanding what it is, but these definitions I'm getting from everyone, they're just ever so slightly too... I don't know what, conceptual? I only know about variables, System.out.println/print(); and nameOfString.length/replace(); So if I could get a real example using those commands, then I think I might understand it. I've heard examples before, but they're always like "If an object is a car, then it has properties and behaviors, it's make, model, color, MPG, turnOn();, turnOff();, blinkerLeft();, blinkerRight();, brake();"
But that just goes over my head. I need an actual implementation of an object that I can see in the code.
By the way, what are implicit parameters and explicit parameters?
Logged
If you haven't already, you MUST listen to the Joe Rogan Experience : http://itunes.apple.com/us/podcast/the-joe-rogan-experience/id360084272

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #8 on: September 04, 2011, 12:19:35 am »

I'll write some code, and comment the crap out of it. Please note that I am just writing it now in a web browser, without bothering to compile and test it, so tell me if it breaks, and it has a good chance of breaking. I go for c# when ever possible these days, it's just faster.



Code: [Select]
Class main
{
static void main(String[] args)
{
Square s = new Square(6); //This makes a new square using it's constructor

s.printDetails(); //This uses a method of the square
system.in.ReadKey(); //This works in java, right? Can't remember, but it should wait for you to press something
}
}

Class Square //This is the blue prints for a square, but not one in itself.
{
private int sideLength; //This is a field, it stores things about the square

public Square(int sideLength) //This is a constructor, it is a method to produce squares.
{
this.sideLength = sideLength;
}

public void printDetails() //This is a method, it does something!
{
system.out.Println("This square has sides that are " + sideLength + "long!");
}
}

eerr

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #9 on: September 04, 2011, 01:02:10 am »

@MaxWhite
The concept of an object in Java is right "On the tip of my tongue" in terms of conceiving of it. I'm so close to understanding what it is, but these definitions I'm getting from everyone, they're just ever so slightly too... I don't know what, conceptual? I only know about variables, System.out.println/print(); and nameOfString.length/replace(); So if I could get a real example using those commands, then I think I might understand it. I've heard examples before, but they're always like "If an object is a car, then it has properties and behaviors, it's make, model, color, MPG, turnOn();, turnOff();, blinkerLeft();, blinkerRight();, brake();"
But that just goes over my head. I need an actual implementation of an object that I can see in the code.
By the way, what are implicit parameters and explicit parameters?

Er, objects are used mostly for storing information.
classes are used mostly for defining methods and objects.

Methods are used mostly for getting shit done.

Code: [Select]
Class hasnothinginginit{}A java class.
It has nothing in it.
kind of a waste considering every class has its own file.

Code: [Select]
Class construct{

construct(){}

}
It has a constructor method.
The constructor method takes no arguments.
It returns an object of type hasconstructor.

Code: [Select]
Class hasnoconstructor{
main (String[] args){
construct x= construct.construct();
Object y=x;
}

}
This class has no constructor
It invokes the constructor method in construct
To make an Object of Type construct.
and make x refer to said object.
Also to make y refer to said object.


implicit means you have to write less when something refers to itself.
In
Code: [Select]
main(){
}
Nothing is implicit. There is no self.


Code: [Select]
class Xray{
int range;
Xray(x){
range=x;
}

}
Range is a variable contained in Xray objects
in method Xray(x), self is the newly created Xray object.

this.range=x is implied in the statment range=x
Logged

Reelyanoob

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #10 on: September 04, 2011, 01:17:06 am »

IRL: "Human" would be the class. Whereas "Bob Jones" would be an object (a specific human). Methods are actions, e.g. verbs. which act on a type of object.

A class is like a blank form, the object is a copy of that form with the blank fields filled in.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #11 on: September 04, 2011, 01:20:45 am »

You want to add some visibility modifiers, eerr? Don't want him picking up bad habits, and as I recall Java is public by default, for some stupid reason.

AllThingsLive

  • Bay Watcher
  • Damn, I'm a sexy bitch!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #12 on: September 04, 2011, 09:50:40 am »

Oh, you nailed it, when you said, "Class Square //This is the blue prints for a square, but not one in itself."
I don't know why that definition did it but none of the others did, but I get it now! An object is something that is being built by the classes and methods, it isn't a physical thing that can be toyed with, but its classes and methods can, right?
Logged
If you haven't already, you MUST listen to the Joe Rogan Experience : http://itunes.apple.com/us/podcast/the-joe-rogan-experience/id360084272

eerr

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #13 on: September 04, 2011, 11:31:56 am »

You can toy with the blueprints of a square

say,
Class Square{
static int defaultsize;
int size;

defaultsize=10


int size will be in any square you make.
most likely, 10 because that is the default size.
Logged

AllThingsLive

  • Bay Watcher
  • Damn, I'm a sexy bitch!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #14 on: September 04, 2011, 08:47:57 pm »

Alright cool I think I got this whole object thing down pat, now we're learning about "new ClassName(parameters);" And that has just blown my teenage mind. But I think I'll get the hang of this one without help, so thank you all again for all the help you provided!
Logged
If you haven't already, you MUST listen to the Joe Rogan Experience : http://itunes.apple.com/us/podcast/the-joe-rogan-experience/id360084272
Pages: [1] 2