Reputation: 99
How do you make a variable class level? What does this term mean? I was told in order to use the String fullname in a different class, I needed to make the variable class level and add a getter method? Can anyone explain what this concept means and how its properly done? The code from the class in question is listed below.
public class SaxHandler extends DefaultHandler {
private String artifactIdTag = "close";
private String versionTag = "close";
private String fullname;
public String getFullname() {
return fullname;
}
private boolean inDependency=false;
private boolean inProperties= false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("properties")) {
inProperties = true;
}
if (qName.equalsIgnoreCase("artifactId")) {
artifactIdTag = "open";
}
if (qName.equalsIgnoreCase("version")) {
versionTag = "open";
}
if (qName.equalsIgnoreCase("dependency")) {
inDependency=true;
}
if (inProperties) {
System.out.println("startElement property");
}
}
private String artifact=null;
private String version=null;
public void characters(char ch[], int start, int length)
throws SAXException {
if (inProperties) {
artifact = new String(ch, start, length);
System.out.println("property characters: "+artifact );
}
if (artifactIdTag.equals("open")) {
artifact = new String(ch, start, length);
version=null;
artifact = artifact.replace("-${org.springframework.version}", "");
System.out.print("artifactId: " + artifact);
}
if (versionTag.equals("open")) {
version = new String(ch, start, length) + ".jar";
version = version.replace("-${org.springframework.version}", "");
String fullname=artifact +"-" +version;
if (inDependency && !(artifact == null || version == null)){
fullname = fullname.replace("-${org.springframework.version}", "");
fullname = fullname.replace("-${spring.security.version}", "");
System.out.printf("%-15s %n", fullname);
FileNameStorage.add(fullname);
}
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("artifactid")) {
artifactIdTag = "close";
}
if (qName.equalsIgnoreCase("version")) {
versionTag = "close";
}
if (qName.equalsIgnoreCase("dependency")) {
inDependency=false;
}
if (qName.equalsIgnoreCase("properties")) {
inProperties = false;
}
}
}
Upvotes: 1
Views: 113
Reputation: 17548
The variable is class level because it is within the scope of the class. However, it has the private modifier, so no other class can access it.
This method is what you want to give another class access to your field:
public class SaxHandler extends DefaultHandler {
...
private String fullname;
public String getFullname()
{
return fullname;
}
...
}
This concept is encapsulation. You want the inner workings of a class to be restricted, so that it can't be broken by things outside of the class. Hence, you provide a getter method to return a reference to the field by value so that it is not modifiable from outside.
Upvotes: 3
Reputation: 2153
Class level could mean one of two things:
The difference between the two is if you use the static modifier or not. if used its a class variable, if not its a member variable. you would use similar modifier on the getter method to indicate if an instance of the class is needed to access the variable.
Upvotes: 0