Member access by dot notation in java:
Having defined the class User, we can construct objects of that class and access its members via a dot notation with an object reference, provided we have indicated that this access is to be allowed.
The keyword public in front of a member in the class User indicates that it may be accessed using dot notation with a User reference variable. In contrast, because we have used the keyword private in front of the User instance variables, there is no access outside the class itself to these variables.
For example, if john references a User object we may not write:
john.numOfAccesses = -1; // not allowed
But, because the updateNumOfAccesses method is public, we may write:
john.updateNumOfAccesses(); // allowed
to increment the instance variable numOfAccesses in the object referenced by john. Notice that we did not provide a setter method for numOfAccesses, which would have looked like this:
public void setNumOfAccesses (int n)
{
numOfAccesses = n;
}
We did not do this because it would allow the same kind of undesirable access (via a method) to the numOfAccesses variable. We have made use of information hiding to ensure that programmers who employ this class can rely on methods to perform their required tasks. A programmer is not allowed to know how data is stored and cannot directly manipulate the data except under very strict rules through the methods.
Separation of concerns:
Of course, setter and getter methods are only the simplest methods we tend to have in our classes, so let us look at some other methods we might have. Let us assume that the application of which the User class is a part requires a method that checks whether a user has made any accesses before. We call this method zeroAccesses: it returns a true value if the user has made no accesses and a false value if he or she has made at least one access.
public boolean zeroAccesses ()
{
return (numOfAccesses == 0);
}
Furthermore, let us also assume that there is a need for a method that compares the user's ID and email address, to see if they are the same. The method returns a true value if the two strings are the same and a false value otherwise.
public boolean isEmailUserID ()
{
return userID.equals(emailAddress);
}
This method makes use of the equals method of String objects and returns true if the strings referenced by userId and emailAddress have the same contents, within the receiving object. We aim to have classes model one kind of object and to have each method deal with one small job, so that it is clear to which class and to which part of the class we should look when we have a particular aim to achieve. We call this separation of concerns. This is one of the motivations for having separate setter and getter methods.
Compilation and run-time errors with null
At compile time you may receive a warning that a local reference variable has not been initialized. You might be tempted at this point to assign the reference variable null to satisfy the compiler that it has been assigned a value, but this is often a mistake. Ask yourself, instead, if the variable is required, or if it should be made to reference some object.
The error may not become apparent until run time, when you may have a program crash with what is known as a NullPointerException. This means that somewhere in your code you have used a reference thinking that it referred to an object, but the reference had the value null.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Member access by dot notation in Java homework and assignments? Live Member access by dot notation in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Member access by dot notation in Java homework help, java assignment help and Member access by dot notation in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours