naming convention for class in java - all caps - java

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.

Related

Naming conventions of composed package names

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.

Naming conventions for static methods, when the class name can be a verb

I have a class called Load, which will contain some static methods for loading resources.
The word "Load" can be both a noun and a verb. So, let's say I want two methods, one to load "Space Files" and one to load "Ocean Files" as an example.
Now, I know that methods should typically be named in such a way where they contain a verb, or describe an action. So, good names for them would be loadSpaceFiles and loadOceanFiles.
However, I understand that good, clean code should try to read like a sentence. The end result of using those names would be code that looks like this:
Load.loadSpaceFiles
//
Load.loadOceanFiles
If I exclude the "load" from the method names, I'll have code like this:
Load.spaceFiles
//
Load.oceanFiles
And that reads much nicer. However, the method names suffer a result, since without the context of the Load class, the method names don't describe the action of the method.
What's considered the best naming convention in this scenario?

Why is java.lang.String capitalized the way it is?

Why is the String class capitalized but java and lang are lowercase, another example would be System.out.println, println is a method so lowercase is expected, but why is out lowercase? Am I missing something or does someone not follow their own rules?
Class names have the first letter in Uppercase. Package names are always in lower case as per naming conventions.
java.lang is the package name
String is the Class name
For the System.out.println() it breaks down as this
System is the class name, so the first letter is Uppercase
out is a public static field which is of the PrintStream class so it can be access directly.
println() is a method of the PrintStream class
Example of usage would be System.out.println("Hello World");
More information about this can be found in the documentation here http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out
System.out.println() here the whole thing is not a method name. Here only the println() is a method name and it is in lower case. System is the class name and out represent an standard object which I don't need to create implicitly.
More over java.lang is a package name and String is the class name. java.lang.String as a whole is not a class name, it's fully qualified class name.Each group in a package name always starts with lowercase.And it's customary in java to write the class and package name by this convention.
"Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code."
Reference: Java Naming Conventions
So, in your example "java.lang" is the package name and "String" is the class name which followed Java Naming Conventions.

Getters/setters when first word has second letter capitalized

What should we expect from the following name? : mGage Program
if I camelCase this it will be mGageProgram and if I generate (in eclipse) the getters and setters I will get the following:
public String getmGageProgram() {
return mGageProgram;
}
public void setmGageProgram(String mGageProgram) {
this.mGageProgram = mGageProgram;
}
Which to me doesn't seem right as I was expecting the getMGageProgram() and setMGageProgram(value).
Are these getters/setters names alright?
According to 8.8: Capitalization of inferred names of the JavaBeans API specification
the names generated from the IDE are correct.
they are 100% correct :) but conventions differ among programmers , for me its method names in camel casing not variables. as for syntax its correct :)
I’d like to provide just a little more depth on what the spec says. It specifies how we get from the name of a getter and/or a setter to a property name. The interesting quote in this context is:
… to support the occasional use of all upper-case names, we check if
the first two characters of the name are both upper case and if so
leave it alone.
It’s from section 8.8: Capitalization of inferred names.
One example given is that URL (as in getURL or setURL) becomes (or stays) URL (not uRL).
So the method names that you and I would have expected, getMGageProgram and setMGageProgram, would have implied a property named MGageProgram with an upper case M. Since we wanted mGageProgram we need to use lower case m in the names of the getter and the setter.
The rules as I read them thus really allow you to use a lowercase letter right after get or set in any getter or setter name. This came as a peculiar surprise to me. Of course it’s not an option that we want to exploit in cases where we don’t have to.
Link: JavaBeans Spec download page

How to make Class.forName ignore lowercase/uppercase

For example when I have a class named;
'MonkeyBusiness'
I know I can call it using Class.forName("MonkeyBusiness");
But when I call it using Class.forName("monkeyBusiness"); or Class.forName("monkeybusiness");
it gives me the exception;
Exception in thread "main" java.lang.NoClassDefFoundError: monkeyBusiness
(wrong name: ntx/gmd/services/usage/MonkeyBusiness)
Is it possible to call it using any case-formatted string? If so, how?
You don't.
Monkey monkey and MONKEY are three totaly different things in Java.
Why do you need such a function? The root problem might be that you don't know which classes are relevant within your application.
There are two things you can do. First, have an internal convention that you only use lowercase or CamelCase class names. In this way, just convert your search to the appropriate format: Class.forName(className.toLowerCase())
Secondly, you can make a cache of all used classes, and look the appropriate name up from cache. Just make a list with the lowercase name as the key, and the real name as the value.
Thirdly, but not recommended, brute force check all combinations. But this creates an awfully bad scaling function for longer names: 2^n, for n-length names.
You can write a ClassLoader that ignores case. Java ClassLoaders basically only support one function: a client can say "Here's a String, please load the class with that name", and the ClassLoader either replies "Sorry, I don't know the class", or "Yes, here is the class". How the ClassLoader implements this behavior is complete up to its implementer.

Categories

Resources