Reputation:
I'm currently studying java and wanted to know what kind of keywords I can type before a variable, a method and a class?
For an example I can declare a variable, method, class like these:
public final int exampleVariable;
public static double exampleMethod () {}
public class exampleClass {}
What else can I write in those codes? I just wanted to know how specifically I can declare a variable, method and a class. Please help.
Upvotes: 0
Views: 55
Reputation: 296
Like @Azhy commented, this website is for solving specific problems not giving tutorials. However, that said, I will do my best to give you the information you need.
public/private/protected:
These keywords can applied to class, variables, or methods. They change the scope of a variable (i.e where the variable can be accessed from). This handy table was taken from this stack overflow question: What is the difference between public, protected, package-private and private in Java?
│ Class │ Package │ Subclass │ Subclass │ World
│ │ │(same pkg)│(diff pkg)│
────────────┼───────┼─────────┼──────────┼──────────┼────────
public │ + │ + │ + │ + │ +
────────────┼───────┼─────────┼──────────┼──────────┼────────
protected │ + │ + │ + │ + │
────────────┼───────┼─────────┼──────────┼──────────┼────────
no modifier │ + │ + │ + │ │
────────────┼───────┼─────────┼──────────┼──────────┼────────
private │ + │ │ │ │
+ : accessible
blank : not accessible
static:
The static keyword can be applied to methods and variables inside classes only. It means that the variable or method is only created once and is shared between all instances of the class. This allows a single value to be shared across multiple objects, and means that static variables and methods must be accessed through the class not an object NOTE: Classes can technically be static too, but only if they are declared within another class declaration, i.e nested classes
Final
This keyword can only be applied to variables, methods, and classes. A final variable is one who's value cannot change. A final class is one which cannot be subclassed. A final method is one which cannot be overridden or hidden by subclasses.
int/double/void/etc.
When applied to variable these define the type of the variable. When applied to methods these define the type the method must output (void means it returns nothing, and can only be applied to methods)
Lastly, I'm sure there are many more which I personally am not aware of. I recommend looking through this list: https://en.wikipedia.org/wiki/List_of_Java_keywords
Upvotes: 0
Reputation: 272795
I don't recommend learning a programming language this way. I think it is way more useful for you to follow a tutorial or a book, and learn the keywords gradually. This is because some keywords are very common whereas others have very limited uses. Knowing all of them at once won't help you much.
Anyway, if you really want to know what goes before a class, method, and variable declaration, you can refer to the Java Language Specification. It describes the Java language in a very precise way. For example, a class declaration is like this, according to the JLS section 8.1:
NormalClassDeclaration:
{ClassModifier} class Identifier [TypeParameters] [Superclass] [Superinterfaces] ClassBody
Since you are asking what can go before class
, I suppose you are interested in the {ClassModifier}
part:
ClassModifier:
(one of)
Annotation public protected private
abstract static final strictfp
This means that before the word class
, there can be 0 or more of the things listed in ClassModifier
.
Note there are also text after this that describes which combinations of the modifiers will cause a compile-time error.
For methods, it's section 8.4. For local variables, it's section 14.4. For fields, it is section 8.3.
If you are confused by the syntax used to describe the syntax of the Java, they are explained in section 2.4.
Upvotes: 1