I have the next situation:
In my current android project, I have packages: api, persistence(local db), ui.
In each of these packages I have classes. For example, classes with the same name Group exist in each package.
package
ui
Group
api
Group
persistence
I need to map api class to ui in some mapper class and the bad things happend:
One class has import on the top of class and another has import inline.
import package.data.Group;
public class MyClass{
package.ui.Group myGroup;
Group myOtherGroup;
}
Want to know if exist some name convention for different modules.
Should I:
keep the name Group and use full class name?
Give my classes another name like GroupEntity, GroupView? If so, is there any convention?
Thanks
You can find all of the naming conventions here:
Here is what you want copied to ensure if the page goes down you still have an answer :
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations.
Examples:
class Raster;
class ImageSprite;
Related
I'm running into a problem where 2 classes from 2 different packages (owned by the same group) have the same name and package statement
Package 1
package com.placeholder.constants.Constants
public class Constants{}
Package 2
package com.placeholder.constants.Constants
public class Constants{}
If I'm working on Package 1 but want to reference this Constants class in Package 2, is this even possible?
Cannot duplicate class name within same package
where 2 classes from 2 different package
But they are not two different packages. You’ve used the same package name for both.
want to reference this Constants class in Package 2
Use either:
A different package name
A different name for your new class
You cannot define two classes with the same name in the same package, for obvious reasons.
The purpose of packages in Java is to create a namespace. Your duplicate class name violates that namespace.
Design problems
package com.placeholder.constants.Constants
That is not a proper package name. You’ve mixed the class name into the package name. The package should be package com.placeholder.constants.
This cross-naming problem of yours raises the possibility that you have a less than optimal class design. You might want to pause a moment to look at the bigger picture.
And if you have that many constants to name, I wonder if some of those should really be enum types. See tutorial by Oracle. Tip: In Java 16 and later, enums can be defined locally, within an a method — a new alternative to being defined as a nested class or defined as a separate class.
You are facing a name conflict. If possible, try to enter the full name of the package.class_location
How do you name a class when it's all caps in Java? For example, if I want to create a class to select certain people to be VIP. Should I name the class "VIPSelector" or "VipSelector"?
Thanks!
Both of your options work. The main goal with classes is to have them start with an Upper Case. So, VIPSelector and VipSelector both work. This convention is mostly used to get rid of a common mistake that you can find in OOP which is when you can't make the difference between a class and a method.
Imagine having a class object called "student", to initiate it, it would be
student s = new student();
That looks a lot like a method and this is why, by convention, we put the first letter in upper case.
This is how class Name should be :
Class names should be nouns, in mixed case with the first letter of
each internal word capitalized. Try to keep your class names simple
and descriptive. Use whole words-avoid acronyms and abbreviations
(unless the abbreviation is much more widely used than the long form,
such as URL or HTML).
Examples: class Raster; class ImageSprite;
Check this for the information : https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html#:~:text=Class%20names%20should%20be%20nouns,such%20as%20URL%20or%20HTML).
Both names are acceptable. The general convention for naming classes in Java is just that the first letter should always be capitalized and the whole name should be in camel case, meaning that the first letter of each word is capitalized.
The google style guide prefers VipSelector
See this answer to a similar question.
I want to create a package named form validator.
Is it better to write
form_validator,
formValidator or
formvalidator?
I want to mention that I want to avoid form.validator. And that form-validator is forbidden.
From the documentation on package naming convention:
Package names are written in all lower case to avoid conflict with the names of classes or interfaces
So this would leave you with the following two possibilities:
form_validator
formvalidator
Actually the documentation also makes it clear that underscore plays a special role when it appears in package names:
if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int" ... the suggested convention is to add an underscore.
So, underscore is suggested only in special cases, into which your naming problem does not seem to fall. So I would recommend formvalidator as the package name.
The most conventional one would be the 3rd one: formvalidator.
The Google Java Style guide notes:
5.2.1 Package names
Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace, not com.example.deepSpace or com.example.deep_space.
As #teppic said, the Oracle Java Docs state that
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
In Java package names are written in all lower case. Which means following would be the most ideal packaging name.
formvalidator
This is also accepted.
form_validator
Package names are written in all lower case to avoid conflict with the names of classes or interfaces. So
form_validator or
formvalidator.
For details see here.
I'm importing a package (in my case, mongodb.DB) into a java file with an identically named class.
In python, I know I can import a module as another name to avoid conflicts. How does Java solve this problem?
It's not feasible to change the name of the class I'm working in.
You say you are "importing a package..." - do you mean you are importing all classes in a package, like "a.b.c.*"? If so, the answer might be to import only those classes you need, not the entire package.
There is no way to import a class as another class.
Hopefully you don't really mean "identically named" as in both of them having the same fully-qualified name. If that's the case, you're screwed, I don't know anything you can do. Hopefully you just mean that the class name is the same in two different packages.
You can extend a class with your own class, and use your new class in the place of the one extended. In other words, if you're importing the class D as in a.b.c.D, and there is another D class, you could extend the first of them (class Z extends a.b.c.D), and then refer to it as Z instead of D. You might need to provide constructors for Z that match ones in D, but no code should be required other than that.
And the fully-qualified names of the classes will always work.
You can use the fully qualified name "mongodb.DB" instead of just the class name.
I am making a project named TruckingCompany, there is a package name truckingCompany, and three sub-packages: utilities, means and objects.
Now I have put the class containing the main method into the utilities sub-package.
Is this correct? Should I put it into the truckingCompany package ( in no sub-package)?
So the generic question is: if there is a package, and some sub-package, and the main method uses classes from all sub-packages, is correct to put the class containing it in a sub-package?
PS: Let me know if the question is not clear.
Yeah, you can just put it in truckingCompany. After all, it is your application's entry point.
As a baseline, from what I've seen so far a good practice is to put the class containing the main method at the top-level package.
In your case, I'd put your class under the package truckingCompany and not in a sub-package.
You should put in truckingCompany but it will work doesn't matter where ever you put it
It is absolutely irrelevant where you put your class. More specifically, a package has no special relation to its subpackages. The packages are basically a flat namespace of package names. I would also like to add that there are some conventions to be followed with package names:
You are supposed to use all lowercase letters;
the package name should be derived from an internet domain name that you own: com.truckingcompany.stuff.morestuff.