Question regarding methods and classes

L

Linus Flustillbe

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
 
M

markspace

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);
 
M

markspace

Oh, and use method overloading, don't need to append "Int" and "Double"
to your method names.

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...
}
}
 
R

Roedy Green

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.
 
L

Linus Flustillbe

<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.
 
L

Linus Flustillbe

Oh, and use method overloading, don't need to append "Int" and "Double"
to your method names.

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?
 
L

Linus Flustillbe

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.
 
M

markspace

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:
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.
 
L

Lew

Linus said:
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.
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.
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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top