In the previous code:
• We declare a member variable called myMemberVariable; because it is declared outside any method, this variable will be a member variable and accessible throughout our class.
• It is declared as static because it will be called from the main method which is already static by default; and as we already know, a static method should only call a method that is also static.
• We then call the method myMethod.
• In the method called myMethod, we set and display the value of the variable myMemberVariable.
• Finally, when the application is launched, the main method is called (as it is the point of entry of our application),
• We set the value of the variable myMemberVariable to 1, and then call the method myMethod.
Please save your code and run your application. You should see a message saying
The value of myMemberVariable before calling the method is 1
The value of myMemberVariable is 3
A few things to remember when you create your applications (checklist)
When you start coding, you will, as for any new activity, make small mistakes, learn what they are, improve your coding, and ultimately get better at writing your applications. As I have seen in the past with students learning programming, there are some common errors that are usually made; these don’t make you a bad programmer; on the contrary, it is part of the learning process because we all learn by trial and error, and making mistakes is part of the learning process.
So, as you create your first application, set any fear aside, try to experiment, be curious, and get to learn the language. It is like learning a new foreign language: when someone from a foreign country understands your first sentences, you feel so empowered! So, it will be the same with Java, and to ease the learning process, I have included a few tips and things to keep in mind when writing your applications, so that you progress even faster. You don’t need to know all of these by now (I will refer to these later on, in the next chapter), but it is so that you are aware of it and also use this list if any error occurs. So, watch out for these: 🙂
•Each opening bracket has a corresponding closing bracket.
•All variables are written consistently (e.g., spelling and case). The name of each variable is case-sensitive; this means that if you declare a variable myvariable but then refer to it as myVariable later on in the code, this may trigger an error, as the variable myVariable and myvariable, because they have a different case (upper-case V), are seen as two different variables.
•As much as possible, declare a variable prior to using it (e.g., int myVar).
•For each variable name, all words (except for the first word) have a capitalized initial. This is not a strict requirement, however, it will make your code more readable.
•For each method, all words (except for the first word) have a capitalized initial. This is not a strict requirement; however, it will make your code more readable.
•All statements are ended with a semi-colon.
•For if statements the condition is within round brackets.
•For if statements the condition uses the syntax “==“ rather than “=“.
•When calling a method, the exact name of this method (i.e., case-sensitive) is used.
•When referring to a variable, it is done with regards to the scope of the variable (e.g., call local variables locally).
•Local variables are declared and can be used within the same method.
•Member variables are declared outside methods and can be used anywhere within a class.
Best practices
As you will start your journey through Java coding, you may sometimes find it difficult to interpret the errors produced by the browser. However, after some practice, you will manage to recognize them, to understand (and also avoid) them, and to fix them accordingly. The next list identifies the errors that my students often come across when they start coding in Java.
Variable naming
• Use meaningful names that you can understand, especially after leaving your code for two weeks.
String myName = "Patrick";//GOOD
String b = "Patrick";//NOT SO GOOD
• Capitalize the initial of all words in the name, except for the first word.
bool testIfTheNameIsCorrect = true;// GOOD
bool testifthenameiscorrect = true; // NOT SO GOOD
Methods
• Use unique (i.e., different) names (for member and local variables).
• Check that all opening brackets have a corresponding closing bracket.
• Comment your code as much as possible to explain how it works.
.
Pages: Page 1, Page 2, Page 3