Java import statement

Status
Not open for further replies.

dingdong-man

Baseband Member
Messages
66
i'm trying to make a simple console application here that would be simplified by importing some class from Java.

import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import static java.lang.Character.isDigit;

public class LetterCheck{
public static void main(String[] args){
char symbol = 'A';
symbol = (char)(128*Math.random());

if (isUpperCase(symbol)){
System.out.println("It is CAPITAL letter ("+symbol+")");
}else {
if(isLowerCase(symbol)){
System.out.println("It is small letter ("+symbol+")");
}else {
System.out.println("It is NOT a letter ("+symbol+")");
}
}
}
}

i just wondering that if i remove the "static" in the import static, it won't work.. when do we put "static" in the import statement and when do not?
 
You use the 'static' keyword in an import statement when importing static methods. However, it is not good style to import methods only because your code becomes less readable. In this particular instance, you should remove all your import statements and explicitly call each static method using the type (or class, if you prefer that terminology) as shown below.
Code:
boolean x = Character.isUpperCase('A');
boolean y = Character.isLowerCase('b');
 
You use the 'static' keyword in an import statement when importing static methods. However, it is not good style to import methods only because your code becomes less readable. In this particular instance, you should remove all your import statements and explicitly call each static method using the type (or class, if you prefer that terminology) as shown below.
Code:
boolean x = Character.isUpperCase('A');
boolean y = Character.isLowerCase('b');
In that case, calling Character.method, Character would be a class and not a type.

But...I didn't know this: you can import static methods? That's funny. :laughing: But anyhow, he did that, didn't he?
 
In that case, calling Character.method, Character would be a class and not a type.
Technically, a class is a type, especially during runtime. To your credit, that terminology isn't used as frequently in the Java world.
 
Technically, a class is a type, especially during runtime. To your credit, that terminology isn't used as frequently in the Java world.
I thought about that..."a class is a type and a type is a class"...but note what I said: "in that case". What does his example have to do with runtime? :rolleyes:
 
What does his example have to do with runtime?
Nothing. My previous post was in response to you, not the OP. Moreover, what have your posts contributed to this discussion at all?

If you really want to split hairs, a class is a "design time" construct, while a type is a runtime construct.
 
Nothing. My previous post was in response to you, not the OP. Moreover, what have your posts contributed to this discussion at all?

If you really want to split hairs, a class is a "design time" construct, while a type is a runtime construct.
Exactly, but remember, he's starting in the Java world, so any tips (at least I always appreciate 'em) are always welcome.

Nonetheless, that was a side-remark to my question at your original reply:
But...I didn't know this: you can import static methods? That's funny. :laughing: But anyhow, he did that, didn't he?
His post does have import static, and since you commented on it, I figured something was wrong? Anyhow...enough.
 
Status
Not open for further replies.
Back
Top Bottom