Results 1 to 20 of 21
Thread: purpose of a static methods
- 02-10-2011, 12:57 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
- 02-10-2011, 01:13 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A static method doesn't require you to have an instance of the class. If you for example create a method to add 5 to some object, you would need to first create an object of the class, then apply the method to an object.
In a static method you don't need to have an instance of the required class, there are many helpful static classes in math, for instance, you can apply Math.sqrt() to any item because it's static.Java Code:class Example{ int x; public void add(Example e){ x + e.x; } public static void main(String[] args){ Example ex = new Example(); Example ex1 = new Example(); ex.x = 5; ex1.x = 10; ex.add(ex1); System.out.println(ex.x); } }
A lot of times when I use static methods, they are used as "helper" methods.
This uses a static method to test if a number is prime, in this examples, you do not need to create an instance of the class Example2.Java Code:public class Example2{ public static boolean isPrime(int n){ int x = Math.sqrt(n); boolean flag = true; for(int i = 2; i< x; i++){ if(n % i == 0){ flag = false; break; } } return flag; } public static void main(String[] args){ System.out.println("Please enter a number: "); int y; Scanner scan = new Scanner(System.in); y = scan.nextInt(); ArrayList<Integer> primes = new ArrayList<Integer>(); for(int i = 1; i <= y; i++){ if(isPrime(i)){ primes.add(i); } } for(Integer num : primes){ System.out.print(num + " "); } } }Last edited by sunde887; 02-10-2011 at 01:16 AM.
- 02-10-2011, 01:19 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Normal if u want to access a method from another class what do u need to do? U have to create an object of that particular class and access the object.
for example u have 2 classes called class1 and class 2, from ur class 2 u want access a method called getCalculate() from class 1, so u have to create an object of class 1 like this
Class 1 ppp=new class1();
and to access that method
ppp.getCalculate();
What static method do is,
u can directly called the method like this, class1.getCalculate()
u no need to create an object of that class which contain the static method. Also the advantag is it reduce the memory wastage in ur PC..Last edited by akramwahid; 02-10-2011 at 01:21 AM.
-
Please elaborate on how it causes a significant reduction in memory "wastage". Are you saying that we should use nothing but static in order to conserve memory, that OOP programming results in nothing but bloat? And how much memory are we saving if I do this in a typical program?
- 02-10-2011, 11:37 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Its ur convenient using static method, otherwise u have to create an object and call the method. Also why i said reducing memory wastage is, if we creating an object means, a separate memory allocation is done by the computer , is it? and ur application going to be with good performance.... Always programming means giving solution for a problem. Good programming means giving solution in an easiest ways, so its ur convenient .....
- 02-10-2011, 11:48 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 02-10-2011, 12:43 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
@akramwahid:
If you find OOP to be bloated and inefficient, what are you doing on a Java board?Ever seen a dog chase its tail? Now that's an infinite loop.
- 02-10-2011, 12:45 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 02-10-2011, 12:49 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Heh, and i gus wrting liek dis savs memry 2 :D
Ever seen a dog chase its tail? Now that's an infinite loop.
- 02-10-2011, 01:30 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
-
My questions were somewhat rhetorical. While you may be somewhat technically correct, that using OOPs techniques adds some overhead to a program's size, and possible speed, and even this is minimized by today's optimizing compilers, the question is does it add a practical overhead, one that is sensed by the user or developer, and the answer in over 99.999% of the time is no, it does not and so your reasons for using statics are not valid.
The other question to ask is what is the rate-limiting-step in software development? Is it code or memory bloat, or is it the difficulty in writing logically organized and readable code that has minimal bugs and that can be extended or enhanced with the least amount of difficulty. The answer in my opinion is the latter, and it is here that using good OOPs techniques can help immensely. YMMV.
- 02-10-2011, 05:38 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
It is a bit of a pity that the JVM specification (and the .class format specification) is a bit technical and not frequently read by people new to programming). Sometimes people even think that objects carry around their own (non static) methods (this is not so). The Java class format is quite a clever one and the vft (Virtual Function Table) format can be initialized (and used) in quite a fast way. There is (almost?) no code bloat in Java, it just offers a better organization of code (and data), that's all. People who are afraid to create another method or another class shouldn't be, there's (almost) no overhead and it adds to the clarity of the code and data big times. Granted, every ease of code generation and data organization comes with a bit of overhead but we don't want to program in assembly language anymore.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2011, 06:46 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 02-11-2011, 08:07 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
Here's my personal favourite (less than two weeks old) "The Spring framework is terribly inefficient because it uses overly long names" ... uttered by some twit who claims to know what he's talking about ... I think Edsger W. Dijkstra was right: a touch of Basic ruins your mind for life ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2011, 08:20 AM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Instant respect killer!
- 02-11-2011, 08:42 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
Oh well, I might as well post my Coding Guide Lines (c) (tm)
1) everything should be public; there's no reason to keep things a secret to other programmers or customers.
2) everything should be static; if everything keeps moving around all the time they're hard to find.
3) variable names, method names and class names should be kept as short as possible. Disk space is expensive and memory is precious; we don't want to waste any of it, do we?
4) interfaces are an invention of the devil: they're slow and obfuscate the code; don't use them.
5) booleans are worthless; we should use 1 and 0 for true or false; or vice versa when it's more convenient for your code but you should always document it clearly in a separate file.
6) use literal constants as much as possible; symbolic constants are a waste of namespace (also see point 3).
7) preferably integer variable names should start with one of the letters in the range i ...n; all other letters indicate a double type.
7) don't use CamelCase; it makes the reader dizzy. Use all lowercase names and prepend them with a mnemonic indicating their type (also see point 7).
8) uppercase names are allowed when you want to shout to the reader; e.g. when something is very important and the reader should pay special attention to it, capitalize.
I hope that this short list guides everyone, who's willing, to clever, fast and efficient code.
kindest regards,
Jos ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2011, 02:12 PM #17
Words? You use words? I write in base64 to keep the code small and concise.
- 02-11-2011, 02:44 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
- 02-11-2011, 02:46 PM #19
You have an audio coupler? That must be nice. I used to just wrap my knuckles against the mercury delay lines in the right sequence to -hard code- my ram. That is, when I wasn't pressing the bits into clay tablets with a reed on the banks of the river near my straw hut. :D
- 02-11-2011, 03:11 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
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 -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 06:56 AM -
Static methods
By carderne in forum New To JavaReplies: 10Last Post: 01-03-2009, 05:27 PM -
Static methods
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks