Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9100 > unrolled thread
| Started by | Linus Flustillbe <admin@nacs.dyndns-office.com> |
|---|---|
| First post | 2011-10-23 03:28 +0000 |
| Last post | 2011-10-23 09:11 +0000 |
| Articles | 12 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
Question regarding methods and classes Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-23 03:28 +0000
Re: Question regarding methods and classes Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-22 20:44 -0700
Re: Question regarding methods and classes Patricia Shanahan <pats@acm.org> - 2011-10-23 06:14 +0100
Re: Question regarding methods and classes markspace <-@.> - 2011-10-22 22:12 -0700
Re: Question regarding methods and classes markspace <-@.> - 2011-10-22 22:14 -0700
Re: Question regarding methods and classes Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-23 13:32 +0000
Re: Question regarding methods and classes markspace <-@.> - 2011-10-23 07:43 -0700
Re: Question regarding methods and classes Lew <lewbloch@gmail.com> - 2011-10-23 08:54 -0700
Re: Question regarding methods and classes Roedy Green <see_website@mindprod.com.invalid> - 2011-10-23 01:37 -0700
Re: Question regarding methods and classes Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-23 13:49 +0000
Re: Question regarding methods and classes Roedy Green <see_website@mindprod.com.invalid> - 2011-11-02 01:23 -0700
Re: Question regarding methods and classes Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-23 09:11 +0000
| From | Linus Flustillbe <admin@nacs.dyndns-office.com> |
|---|---|
| Date | 2011-10-23 03:28 +0000 |
| Subject | Question regarding methods and classes |
| Message-ID | <4ea389df$0$13444$9a566e8b@news.aliant.net> |
Does a method have to be part of a class that gets instatiated or can it
live in a class to be available to all other classes without
instantiation...it's available when it's imported?
I created a package called helperClasses and a class like this
package helperClasses;
public class rType {
public boolean isBetweenInt(int a, int b, int c)
{
return (a <= c && a >=b);
}
public boolean isBetweenDouble(double target, double lower, double upper)
{
return (target <= upper && target >= lower);
}
}
Is there a way to make the methods contained in rType avaiable by simply
importing the class or does it have to be instantiated? I'd like to be
able to, in a Java program, execute the following lines
import helperClasses;
public class test {
public static void main(String[] args) {
boolean isBetween = inBetweenInt(4,6,87);
System.out.println(isBetween);
}
}
In this case, it would say "false"
Here is my working tester
import helperClasses;
class rTypeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
rType myTest = new rType();
boolean hello = myTest.isBetweenInt(4,6,87);
System.out.println(hello);
}
}
And actual output
false
Just seems like extra work and..even worse... if you create an instance
of rType and it has 250 methods, isn't that gonna potentially slow
things down.
So basically what I want to do is create a whole bunch of methods, put
them in one class and have them all available on demand just by
importing the class...no instantiations required.
Make sense? This is probably silly so if it is, just ignore me..I'll
go away
--
****************************************************************
* Usenet Impovement Project http://nacs.dyndns-office.com/usenet
****************************************************************
[toc] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2011-10-22 20:44 -0700 |
| Message-ID | <F_GdndVSBOITED7TnZ2dnUVZ_jidnZ2d@posted.palinacquisition> |
| In reply to | #9100 |
On 10/22/11 8:28 PM, Linus Flustillbe wrote: > [...] > So basically what I want to do is create a whole bunch of methods, put > them in one class and have them all available on demand just by > importing the class...no instantiations required. > Make sense? This is probably silly so if it is, just ignore me..I'll > go away Not silly at all. If I understand your question correctly, you are asking about "static" methods (not to be confused with "static nested classes" :( ). That is, a method that can be called using the type name, rather than an instance reference. They can be quite useful, when you need to implement some functionality that does not depend on some specific instance data. You can also have static data members; those members are, like the static methods, referenced using the type name rather than an instance reference. See http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html Pete
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-10-23 06:14 +0100 |
| Message-ID | <SJidnVhi34siPz7TnZ2dnUVZ_tGdnZ2d@earthlink.com> |
| In reply to | #9101 |
Peter Duniho wrote: > On 10/22/11 8:28 PM, Linus Flustillbe wrote: >> [...] >> So basically what I want to do is create a whole bunch of methods, put >> them in one class and have them all available on demand just by >> importing the class...no instantiations required. >> Make sense? This is probably silly so if it is, just ignore me..I'll >> go away > > Not silly at all. If I understand your question correctly, you are > asking about "static" methods (not to be confused with "static nested > classes" :( ). That is, a method that can be called using the type > name, rather than an instance reference. ... Also, see the class java.lang.Math for an easily accessible example of this in practice. Patricia
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-22 22:12 -0700 |
| Message-ID | <j807nt$8h6$1@dont-email.me> |
| In reply to | #9100 |
On 10/22/2011 8:28 PM, Linus Flustillbe wrote:
> Does a method have to be part of a class that gets instatiated or can it
> live in a class to be available to all other classes without
> instantiation...it's available when it's imported?
> package helperClasses;
> public class rType {
private rType() {}
public static boolean isBetweenInt(int a, int b, int c)
> {
> return (a<= c&& a>=b);
> }
public static boolean isBetweenDouble(double target, double lower,
double upper)
> {
> return (target<= upper&& target>= lower);
> }
> }
import static helperClasses.rType.*;
>
> class rTypeTest {
> public static void main(String[] args) {
boolean hello = isBetweenInt(4,6,87);
> System.out.println(hello); // weird choice of variable names...
> }
> }
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-22 22:14 -0700 |
| Message-ID | <j807ru$8h6$2@dont-email.me> |
| In reply to | #9102 |
Oh, and use method overloading, don't need to append "Int" and "Double"
to your method names.
On 10/22/2011 10:12 PM, markspace wrote:
>> package helperClasses;
>> public class rType {
>
> private rType() {}
>
> public static boolean isBetween(int a, int b, int c)
>> {
>> return (a<= c&& a>=b);
>> }
> public static boolean isBetween(double target, double lower,
> double upper)
>> {
>> return (target<= upper&& target>= lower);
>> }
>> }
>
>
> import static helperClasses.rType.*;
>>
>> class rTypeTest {
>> public static void main(String[] args) {
>
> boolean hello = isBetween(4,6,87);
>> System.out.println(hello); // weird choice of variable names...
>> }
>> }
>
[toc] | [prev] | [next] | [standalone]
| From | Linus Flustillbe <admin@nacs.dyndns-office.com> |
|---|---|
| Date | 2011-10-23 13:32 +0000 |
| Message-ID | <4ea4177e$0$19700$9a566e8b@news.aliant.net> |
| In reply to | #9103 |
On 2011-10-23, markspace <-@> wrote:
> Oh, and use method overloading, don't need to append "Int" and "Double"
> to your method names.
>
> On 10/22/2011 10:12 PM, markspace wrote:
>
>>> package helperClasses;
>>> public class rType {
>>
>> private rType() {}
>>
>> public static boolean isBetween(int a, int b, int c)
>>> {
>>> return (a<= c&& a>=b);
>>> }
>> public static boolean isBetween(double target, double lower,
>> double upper)
>>> {
>>> return (target<= upper&& target>= lower);
>>> }
>>> }
>>
>>
>> import static helperClasses.rType.*;
>>>
>>> class rTypeTest {
>>> public static void main(String[] args) {
>>
>> boolean hello = isBetween(4,6,87);
>>> System.out.println(hello); // weird choice of variable names...
>>> }
>>> }
>>
>
So that works really well.
package helperClasses;
public class rType {
public static boolean isBetween(int a, int b, int c)
{
return (a <= c && a >=b);
}
public static boolean isBetween(double target, double lower, double upper)
{
return (target <= upper && target >= lower);
}
}
import static helperClasses.rType.*;
class rTypeTest {
public static void main(String[] args) {
boolean hello = isBetween(4,6,87);
System.out.println(hello);
boolean hello2 = isBetween(5.4, 2.3, 9.2);
System.out.println(hello2);
}
}
false
true
I understand the concept of overloading methods... I haven't done much
Java in few years so I'm taking a refresher by going through the Java
Tutorial. What does adding the "static" modifier to the import
statement do? Since the methods in the rType class are already defined
as being static (only one instance no matter how many times the class is
instantiated ... see I read up on that) why do we need the modifier
except to make the code compile? Or is that the only reason.. because
Java needs it?
--
****************************************************************
* Usenet Impovement Project http://nacs.dyndns-office.com/usenet
****************************************************************
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-23 07:43 -0700 |
| Message-ID | <j8196s$rp5$1@dont-email.me> |
| In reply to | #9113 |
On 10/23/2011 6:32 AM, Linus Flustillbe wrote: > I understand the concept of overloading methods... I haven't done much > Java in few years so I'm taking a refresher by going through the Java More on overloading: <http://download.oracle.com/javase/tutorial/java/javaOO/methods.html> > Tutorial. What does adding the "static" modifier to the import > statement do? Since the methods in the rType class are already defined > as being static (only one instance no matter how many times the class is > instantiated ... see I read up on that) why do we need the modifier > except to make the code compile? Or is that the only reason.. because > Java needs it? By default "import" brings in class definitions, not the members of the class. So import helperClasses.rType; would import the class, and you'd have to use rType.isBetween( x, y, z); C.f. that link Peter gave you, which uses this style of static method access. "Import static" is a special import syntax that imports only the static members of rType, not the class definition, so that you can use its static methods and fields "bare," without a class name as a qualifier. It's a convenience for typing (and reading), nothing else.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-23 08:54 -0700 |
| Message-ID | <22389553.296.1319385241947.JavaMail.geo-discussion-forums@yqnv12> |
| In reply to | #9113 |
Linus Flustillbe wrote:
> So that works really well.
>
> package helperClasses;
By widespread but not quite universal convention, package names should be all lower case.
> public class rType {
By universal convention, class names should begin with an upper-case letter.
<http://www.oracle.com/technetwork/java/codeconv-138413.html>
> public static boolean isBetween(int a, int b, int c)
> {
> return (a <= c && a >=b);
PLEASE DO NOT INDENT USENET CODE POSTS WITH TABS!
> }
> public static boolean isBetween(double target, double lower, double upper)
Why did you switch to intelligent variable names here, but not in the other method?
> {
> return (target <= upper && target >= lower);
PLEASE DO NOT INDENT USENET CODE POSTS WITH TABS!
> }
>
> }
>
> import static helperClasses.rType.*;
> class rTypeTest {
By universal convention, class names should start with an upper-case letter.
> public static void main(String[] args) {
> boolean hello = isBetween(4,6,87);
> System.out.println(hello);
> boolean hello2 = isBetween(5.4, 2.3, 9.2);
> System.out.println(hello2);
> }
> }
>
>
> false
> true
>
> I understand the concept of overloading methods... I haven't done much
> Java in few years so I'm taking a refresher by going through the Java
> Tutorial. What does adding the "static" modifier to the import
> statement do? Since the methods in the rType class are already defined
Read the manual.
GIYF.
<http://lmgtfy.com/?q=Java+import+static>
> as being static (only one instance no matter how many times the class is
> instantiated ... see I read up on that) why do we need the modifier
> except to make the code compile? Or is that the only reason.. because
> Java needs it?
The "import static" directive lets you import static (get it?) members from another class. This obviates having to prefix those members with the class and the dot operator.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-23 01:37 -0700 |
| Message-ID | <2fk7a71akc2k7fr1ennkiql4um7sbnbp2h@4ax.com> |
| In reply to | #9100 |
On 23 Oct 2011 03:28:31 GMT, Linus Flustillbe <admin@nacs.dyndns-office.com> wrote, quoted or indirectly quoted someone who said : >Does a method have to be part of a class that gets instatiated or can it >live in a class to be available to all other classes without >instantiation...it's available when it's imported? You can have static methods that can be called without creating any objects. The class itself will get loaded and an invisible class object will be created with slots for all the static variables. This is a very basic feature of Java. I suggest you get an text, even a badly out of date one, to explain such things. see http://mindprod.com/jgloss/gettingstarted.html -- Roedy Green Canadian Mind Products http://mindprod.com It should not be considered an error when the user starts something already started or stops something already stopped. This applies to browsers, services, editors... It is inexcusable to punish the user by requiring some elaborate sequence to atone, e.g. open the task editor, find and kill some processes.
[toc] | [prev] | [next] | [standalone]
| From | Linus Flustillbe <admin@nacs.dyndns-office.com> |
|---|---|
| Date | 2011-10-23 13:49 +0000 |
| Message-ID | <4ea41b5f$0$19700$9a566e8b@news.aliant.net> |
| In reply to | #9107 |
On 2011-10-23, Roedy Green <see_website@mindprod.com.invalid> wrote: > On 23 Oct 2011 03:28:31 GMT, Linus Flustillbe ><admin@nacs.dyndns-office.com> wrote, quoted or indirectly quoted > someone who said : > >>Does a method have to be part of a class that gets instatiated or can it >>live in a class to be available to all other classes without >>instantiation...it's available when it's imported? > > You can have static methods that can be called without creating any > objects. The class itself will get loaded and an invisible class > object will be created with slots for all the static variables. > > This is a very basic feature of Java. I suggest you get an text, even > a badly out of date one, to explain such things. > see http://mindprod.com/jgloss/gettingstarted.html Thanks Roedy. I was just getting ahead of myself; thinking about "what if" when I was still on the "what" topic. As you can see from other reponses to my question and my replies, I have figured it out for the most part but still don't understand why I have to modify the import statement... someone will probably say "that's just the way it has to be" and if that's the case, that's fine. -- **************************************************************** * Usenet Impovement Project http://nacs.dyndns-office.com/usenet ****************************************************************
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-11-02 01:23 -0700 |
| Message-ID | <9av1b71qv232dmjitoi4kbl1eb873qdn8u@4ax.com> |
| In reply to | #9114 |
On 23 Oct 2011 13:49:19 GMT, Linus Flustillbe <admin@nacs.dyndns-office.com> wrote, quoted or indirectly quoted someone who said : > >Thanks Roedy. I was just getting ahead of myself; thinking about "what >if" when I was still on the "what" topic. As you can see from other >reponses to my question and my replies, I have figured it out for the >most part but still don't understand why I have to modify the import >statement... someone will probably say "that's just the way it has to >be" and if that's the case, that's fine. We were all newbies to start. I have been figuring out Java, asking endless questions and explaining this to newbies since the days of Java 1.0. I have been collecting explanations geared toward newbies to programming and newbies to Java and posting them in the Java glossary. See http://mindprod.com/jgloss/jgloss.html If you have trouble with something, often just reading one entry will disabuse you of one of the common misconceptions that his throwing you off. Also chasing links at the bottom to related topics can be fruitful. Often the material you want is not filed where you expected it. -- Roedy Green Canadian Mind Products http://mindprod.com Capitalism has spurred the competition that makes CPUs faster and faster each year, but the focus on money makes software manufacturers do some peculiar things like deliberately leaving bugs and deficiencies in the software so they can soak the customers for upgrades later. Whether software is easy to use, or never loses data, when the company has a near monopoly, is almost irrelevant to profits, and therefore ignored. The manufacturer focuses on cheap gimicks like dancing paper clips to dazzle naive first-time buyers. The needs of existing experienced users are almost irrelevant. I see software rental as the best remedy.
[toc] | [prev] | [next] | [standalone]
| From | Linus Flustillbe <admin@nacs.dyndns-office.com> |
|---|---|
| Date | 2011-10-23 09:11 +0000 |
| Message-ID | <4ea3da41$0$14660$9a566e8b@news.aliant.net> |
| In reply to | #9100 |
On 2011-10-23, Linus Flustillbe <admin@nacs.dyndns-office.com> wrote: <snipping my own stuff here> Thanks all.. I just haven't gotten far enough into the tutorial yet I guess since it seems what I am trying to do (static methods) is covered later on. If I had known what these things are called I might have been able to discover this without posting. -- **************************************************************** * Usenet Impovement Project http://nacs.dyndns-office.com/usenet ****************************************************************
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web