Re: Static Versus Non Static
"Ravi Shankar Nair" <sujashankar@pacific.net.sg> wrote in message
news:eebjo5$jp4$1@nobel.pacific.net.sg...
[...]
A static method cannot access non static members of the class. Well, thats
fine and clear.
But a non static method ( or instance method) can access a static variable
in Java!!!
But a non static method is basically doing an instance service. Hence how
come instance service be allowed to manipulate a class variable ( or
static variable ) ? Surely C++ throws an exception, and is that not a
drawback in Java language syntax?
Does C++ throw an exception? I'd be surprised. Did you actually try
this?
Or anyone please suggest a situation where instance service has to
manipulate a class variable ? Constructor makes sense, and let
constructors be the only functions which has the power to do both instance
and class service. But other than constructors, every other instance
method should be disallowed to use a class variable, right?
We're implementing an XML view for one of our data structures. We use
the Saxon XQuery library to be able to perform queries on XML documents. The
Saxon library expects every XML node to implement the INode interface, and
one of the methods in INode is getName():
public interface INode {
/**
* Returns the name of the node. For example, the
* name of the node
* <foo>Hello World!</foo>
* is "foo".
*/
public String getName();
}
So our data structure has a bunch of classes, and the instances of these
classes will always return the same name for the INode interface.
public class EmployeeNode implements INode {
public final static XML_NODE_NAME = "Employee";
public String getName() {
return XML_NODE_NAME;
}
}
- Oliver