• Post Reply Bookmark Topic Watch Topic
  • New Topic

why cant we put anything outside classes ?  RSS feed

 
deepak mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i couldnt think about any other title,as i am getting more and more confused.
this is what i know :
an object is "something" which has got some functionality && exists in memory (thats why real-world).
why cant we create objects of "only" methods ?

to be exact,i cant understand why java needs to put everything in the class?
eg.

public static void main(String args[])
{
int i;
i=i+5;
}


this method should be able to form an object, it has got both data members, and it can call another function to modify the data.
why do we NECESSARILY need to put this in a class ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is intended to be an Object Oriented programming language. Depending on your definition of this term, it probably doesn't succeed 100% in this (it's use of primitive types being the most oft quoted example), but ensuring every bit of code is part of a class does help towards this goal.

Put another way, the developers of Java obviously saw no advantage in having code outside of classes, so chose not to allow it. Why do you think it would be useful to have such code ?

Originally posted by deepak mishra:
why cant we create objects of "only" methods ?


You can. Just make all the methods static. Take a look at the Math class for an exanmple of this.
 
deepak mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can. Just make all the methods static. Take a look at the Math class for an exanmple of this.


is that really possible ?
will my program work if i DO NOT put all of my STATIC methods in ANY class ?
that way, there would be no need to put the main() method in any class, right ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not allow methods to exist outside a class, so you will have to put them into a class. It's just how Java is created.

As for the method-only class, it's a good idea to make it final and give it a private constructor:

(Note: that's a literal copy-paste from java.lang.Math)

This will prevent you from creating instances, which you won't need anyway because all your methods are static.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deepak mishra:


is that really possible ?


Yes, not recommended, but possible.

Originally posted by deepak mishra:
will my program work if i DO NOT put all of my STATIC methods in ANY class ?


You are confusing Class with Object. Your methods must belong to a Class, which is the basic compilation unit for Java so you can't get around them. You don't need Objects, though, which are instances of a class.

So Joanne's response was to the quote:
"why cant we create objects of "only" methods ?"
And her answer that you don't need Objects is correct. Some code as an Example:


The method getNumber is static, so it belongs to the class named MyClass and can be called like this:

But the method getValue is not static, it is an instance method, belongs to an Object, and so it needs an Object to call it on
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:
Yes, not recommended, but possible.

I disagree. I have several utility classes - just methods that perform pieces of code I would otherwise execute regularly. These methods do not belong to any other class, and retyping the code is a stupid thing to do. Ergo: utility class.
 
G Estes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, maybe using a delegate or decorator design pattern would be an even better way to do this. But to your utility classes, when you instantiate them (albeit via static or actual instantiation) the class instance is an object...

Or am I misunderstanding something here?

Cheers!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deepak mishra:
is that really possible ?
will my program work if i DO NOT put all of my STATIC methods in ANY class ?
that way, there would be no need to put the main() method in any class, right ?


you can have a class with only static methods. but you would need to put a main method SOMEWHERE (and remember main is also static).

a java program always starts at a main() method. if there is no main defined, you program has no starting point.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

I disagree. I have several utility classes - just methods that perform pieces of code I would otherwise execute regularly. These methods do not belong to any other class, and retyping the code is a stupid thing to do. Ergo: utility class.


I was not talking about a Utility class, there are many cases that benefit from having a Class with all static methods.

But the OP was quoting a line that said 'make all your methods static' - which I interpreted as his entire application structure in static methods. Something you could do but I don't think is the best way to program in Java.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah then I misunderstood you, and you are right. If you're using only static methods that's not object-oriented programming - that's procedural programming. You might as well stick to C or Pascal for that.
 
deepak mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your responses !!

Originally posted by Steve Luke:

You are confusing Class with Object.

yes..you caught me steve !
this is a terrible problem i would like to solve.
i imagine an "Object" to be anything that is being "executed" and provides a set of functionality over a set of data.
unluckily i find a "method" to be implementing the same definition.

but after all your explanations, i feel that they ARE indeed the same in reality...its only that the java developers tried to use different names, and created the concept of objects, in order to stick to OOPS.
did i get it right ??
 
deepak mishra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am waiting for a confirmation
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deepak mishra:

did i get it right ??


No, not at all, not even close.

A class is like a category or "type" of thing: Car, House, Person.

An object is a specific instance of a class: thatRedCarOverThere, myHouse, you.

A "method" is like a function in a non-object oriented language, except that it's "attached" to a specific object. If I call myHouse.turnLightsOn(), then the lights in that one particular house go on. The method turnLightsOn() would be defined by the class House, but when you call it, you call it "on" an object, and it operates on that specific object.

A "static method" is a method that's not attached to a particular object, and as such, it's very similar to a C function -- except that as it's defined inside a class, it still has that class for a context.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!