Can we name a class with a Java keyword? - java

What happens if we use a Java key word as our class name. In my case I created a class "Process". But I can not instantiate it. Is there a way to do it?

You cannot use Java keywords as identifiers in a Java program (to name a class, or a variable for example). However, you did not use a Java keyword, rather you named your class Process just like the java.lang.Process class. Since the java.lang.* package is imported by default in any java class, that is why you cannot instantiate it.
If you really want to instantiate your own Process class just refer to it with its fully qualified name, like: new my.package.Process(....).
But I advise you to change the name, it will give your code more clarity.

Related

Cant use a public method from another class in java

I am trying to call a public constructor of a public class located in one package of my project from the main class, located in a class from another package different to the first one, but located in the same project.
I have understood that the public modifier grant you access to methods from any location inside or outside the package, so i just try to create and instance a new object of the public class first mentioned.
F.E: I try something like.... ClassName newObject = new ClassName(); from the main class
Actually, in order to be able of using that (im using netbeans IDE) I need to import the class/method I want to call, even if they have the public modifier.
My question is... is there a way of using these methods without the need of importing them to the main class ?
I am new in this webpage, so sorry if there is anything wrong with the question.
Qualification and visibility have little to do with eachother. Those are different concepts.
Visibility (enforced by access modifiers like public, protected, private and package-private) is about which class may access (i.e. call or use) it.
Qualification is about the compiler asking you: "Okay, you are mentioning a class name, but there could be thousands around with that name. Which one do you mean?"
You could use the fully qualified class Name instead.
a.b.c.MyClass myClass = new a.b.c.MyClass();
Also see
Java: import statement vs fully qualified name?
Java compiler restricts its search for classes inside the package only. In order to make use of any class belonging to a different package, you have to import it explicitly. You can read more about packages here.
Access specifiers are more from restricting the methods from being accessible by outside world. These access specifiers enforce further restrictions on top of what is enforced by packages. You can refer to this link for access specifiers.

.class - what is this construction and how it works?

I am beginner in programming and i just started working with greenfoot (software for creating games using Java). When i was writing code i had to use construction builded in greenfoot which was using parameter described as: java.lang.class where i had to type ClassName.class . I was trying to go through documentation and a lot of other sources to figure out how it works and what is this, but to be honest i couldnt understand anything. Is there is someone who can tell me in simply words what does it mean and how construction like ClassName.class works? That is the first time i see dot notation used like this, before I have seen only dot notation when i tried to refer to for example other class method like: OtherClass.method() . So is it just builded in instance variable of every class or something like this? Thanks in advance
It's called a class literal. ClassName.class provides access to the instance of Class that represents the given class. So for instance, String.class refers to the Class instance for the String class. Using that object, you can do do various things (like find out what its members are at runtime via reflection). (FWIW, objects provide also access to the Class instance for their class via their getClass method.)

Elegant way to avoid long object names (Fully Qualified Class Name)

I have a class where I use two objects that happen to have the same name.
One is :
com.google.api.services.calendar.model.Event
and the other 3ed party object with the same name, say:
com.some.other.package.Event
Using import for both objects is not a good option because they will mask each other.
Using the very longggggg names all over the code does not look good either.
Creating a "dummy" type just for the sake of changing its name:
public class CEvent extends com.google.api.services.calendar.model.Event {}
does not seem like an elegant solution.
How can I preserve the original object name (Event) yet use a shorter path name ?
In Java it is impossible, the only way is to use full qualified names of classes. However, you can do this in other JVM based languages, such as Scala:
import com.some.other.package.Event => OtherEvent
or Groovy
import com.some.other.package.Event as OtherEvent
Java provides 2 ways:
Use Fully Qualified Class Name for each class, which you don't want to
Use class name for One class and Fully Qualified Class Name for other
Alternate, is to Sub-class the other class, and then you can use the new subclass name. (import sub-class)
You can't shorten class names in Java; you can either import a class name (to use it without qualifiers) or use the fully qualified name. So at least one of the Event classes will have to be referred to by its fully qualified name. (Unless, as you say, you subclass one of them just to save on typing.)
The Java tutorials address this when discussing name ambiguities:
If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name.

Can you name an abstract class Object?

Considering everything is object oriented etc, so names have to describe the object and what it is, I have an abstract class that sub classes inherit from. These are all objects on the screen (it's a game), i.e, player, and a weight (trapezoid weight). I have it currently named Character but it doesn't seem fitting as the weight is not a Character itself, it only inherits properties from Character.
Could I call this class "Object" without it breaking conventions? - could someone come up with a more appropriate name?
Technically, you could - but it's a very, very bad idea, so don't.
Longer explanation: The Object class already in Java is java.lang.Object - so there's no technical reason why you could create another Object class in another package, just as you could create another String class in another package. (Actually, technically speaking you could even create your own java.lang.Object, but I'm not even going to go there!)
However:
Could I call this class "Object" without it breaking conventions?
Without breaking convention? Not in the slightest. You should never duplicate such commonly used class names elsewhere, especially those in java.lang. It would be considered incredibly bad code design.
In terms of a better name, Actor or Sprite may be two good alternatives.
Java's Object class is part of the java.lang package which is automatically imported for every class file. If you name your class Object and forget to explicitly import it in other classes, you will have issues, thinking you're using com.custom.Object (your class), but actually using java.lang.Object, the JDK's.
Use a more descriptive name, ApplicationObject.
Yes you can. The class beside the name has the path that is package.
package org.stackoverflow
public class Object {
}
By default java.lang is prohibited package name so you can not do declare
package java.lang
public class Object {
}
The class names does not have to be unique in scope of whole world. Using the class path you are able to override the JVM definition of class.

Is "set" a keyword for Java?

Beginner on Java, I got this piece of code from .java file from a big project which compiles
for (Var var : cfg.getArgs())
set(var);
but in my Java program there is no definition of that set method. I am just wondering whether set is a keyword of Java?
No, set is not a keyword in Java.
Member function
What you see in that snippet is a call to a method called set. Presumably a member method of the enclosing class, in which case it is identical to the more familiar syntax:
this.set(var);
Look above or below that particular snippet, and you'll most likely find the definition. It should look something like
public void set(Var var) {
....
}
Member function of super class
Note that it can also be a method of a super class. I.e., if the class is declared as
class Configuration extends SomeClass { ...
then the definition of the set method could be in SomeClass.
Here is the official list of keywords btw,
Java Language Keywords
Statically imported method
If it's not defined in the class, and you can't for your life find it in any super class, then it may be a statically imported method from some other class. In that case, look for something like
static import org.comp.Class.set;
in the top of the .java file.
Finally, if you're using Eclipse to develop, you can hit F4 or right click on the identifier and select declarations in project to jump to the definition of the method.
set is likely the name of another method in the Java code you borrowed.
According to this list, no set isn't a keyword:

Categories