I just had some method calling scenarios I was unsure of, and was hoping someone could help clear some up for me.
a) If I was in the SalesMethod class and I wanted to call the sales method from the region method how would I do that? (private method calling public method)
b) What about sales calling purchase? (public calling public from within same class)
c)If I was in SalesMethod, what would be a way to call the futureSales method? Would I have to create an instance for it since it's non static?
Thanks in advance.
public class SalesMethod
{
public static double sales ()
{
code
}
private static void region ()
{
code
}
public static double purchase ()
{
code
}
public void futureSales ()
{
code
}
}
a) private method calling public method is ok since public mean "visible from everywhere".
public static double region()
{
sales();
}
b) public method calling public method is ok for the same reason.
b') public method calling private method is ok if the private method is in the same class than the public one.
c) to call a non-static method, you have to create an instance since you call it "on" an object. You can't call it from a static method the way you do in the example above.
static means "relative to a class"
non-static is relative to an object, you can see that as an action performed by the object.
If I was in the SalesMethod class and I wanted to call the sales method from the region method how would I do that? (private method calling public method)
They are both static, so you can call them everytime you need to.
sales();
// Or
SalesMethod.sales();
What about sales calling purchase? (public calling public from within same class)
They are both static, so you can call them everytime you need to.
purchase();
// Or
SalesMethod.purchase();
If I was in SalesMethod, what would be a way to call the futureSales method? Would I have to create an instance for it since it's non static?
Yes.
SalesMethod instance = new SalesMethod();
instance.futureSales();
Related
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType = product;
}
// advertise method
public void advertise() {
String message = "Selling " + productType + "!";
System.out.println(message);
}
// main method
public static void main(String[] args) {
String cookie = "Cookies";
Store cookieShop = new Store(cookie);
cookieShop.advertise();
}
}
In this class, the constructor for the class is called in its own main method. Why wouldn't this recursively call itself infinitely?
EDIT: From the future, yes; this quite a noob question that could be, and has been solved by reading the docs.
Constructors of a class are used to construct class-instances. Static methods are methods of the class, not of its instances (although they can be accessed through its instances). A class is loaded through a ClassLoader. Most classes are loaded and constructed through the ClassLoader.getSystemClassLoader(), the JVM takes care of constructing the class.
The class Store is already loaded when main(...) is executed and the call to the constructor creates, as explained above, an instance of that class, not the class itself. Thus, no recursive calls occur.
As was pointed out by #AndyTurner, even if an instance method were to call a constructor, this would not necessarily lead to a recursion. The constructor constructs a new instance, the old instance is decouple from the new instance.
Main method is a static method and it is called one time when application is starting. Once application started, main method can't run again.
When you making a store object, It is true that constructor is running. But it does not calling main method again.
So I have a public class and within this class are several public functions including a static method.
public class TestVocabValidator {
static { getEnumList( vocabList.values() ); }
public static Iterator<String> getVocabEntries(String x) {
return null;
}
}
Whenever I call on the function getVocabEntries(), does the static method get called automatically?
The static blocks (e.g. static {...}) are executed once, when the class' name is referenced and the class is loaded.
The static methods (e.g. getVocabEntries) are executed every time they are invoked.
Not whenever you call that static method. The first time that class is loaded, in this case the first time you call that method, the static block is called.
Why does the following code print "YO"? Whose printYo() is being called? I would think that this code would not compile because printYo() is private to t.
public class Test {
private void printYo() {
System.out.println("YO");
}
public void doubleTrouble(Test t) {
t.printYo();
}
public static void main(String[] args) {
Test test = new Test();
test.doubleTrouble(new Test());
}
}
What can I do to make sure the outer object doesn't mutate the argument class?
printYo() is private to t
No. That method is private in regards to the class Test. Any piece of code within Test can use it.
What can I do to make sure the outer object doesn't mutate the argument class?
Java does not have any built in mechanism to refuse access to members on a per instance basis. (If that is what you meant.)
You are calling the method with in the class , which sound correct for the output . Even if you call the main method from different class it gives the same output.
I tried to access a variable in the main class from another class. I don't know if it's possible but I need those variables. If there is a way please show me. This is the simple example.
public class A(){
public static String status;
public static void main(String [] args){
Scanner s = new Scanner(System.in);
System.out.println("Deadlock or no deadlock (y/n)");
status = s.nextline();
......
}
Then I want to use this variable "status" in another class which implements a runnable (thread). If status is "y" then a particular block of codes (if/else) inside the run methon will executes.
Anyone point me how do I call the main class so I can access the variable status from my the runner class. Thanks.
The field status in class A is public and static, so you can call it from wherever you like.
public class B {
public void myMethod() {
System.out.println("A's status is " + A.status);
}
}
A point on terminology: A here is a class, which represents an object. main happens to be a method within that class A. So you'd say that status belongs to A, not to main.
No java is pass by value not pass by reference. You could follow the example I'm linking and see if that helps though.link You could make it static and it would be usable that way but any operations on it would change when ever you perform any on it hence static.
public class CallingStaticMethod {
public static void method() {
System.out.println("I am in method");
}
public static void main(String[] args) {
CallingStaticMethod csm = null;
csm.method();
}
}
Can someone explain how the static method is invoked in the above code?
It's been optimized away by the compiler, simply because having an instance of the class is not necessary. The compiler basically replaces
csm.method();
by
CallingStaticMethod.method();
It's in general also a good practice to do so yourself. Even the average IDE would warn you about accessing static methods through an instance, at least Eclipse does here.
Java allows you to use a Class instance to call static methods, but you should not confuse this allowance as if the method would be called on the instance used to call it.
instance.method();
is the same as
Class.method();
The java language specification says the reference get's evaluated, then discarded, and then the static method gets invoked.
15.12.4.1. Compute Target Reference (If Necessary)
This is the same behavior when you use the this reference to call a static method. this gets evaluated then discarded then the method is called.
There is even an example in the specification similar to your example.
When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null:
class Test1 {
static void mountain() {
System.out.println("Monadnock");
}
static Test1 favorite(){
System.out.print("Mount ");
return null;
}
public static void main(String[] args) {
favorite().mountain();
}
}
Well this is perfectly ok. The static method is not being accessed by the object instance of class A. Either you call it by the class name or the reference, the compiler will call it through an instance of the class java.lang.Class.
FYI, each class(CallingStaticMethod in the above illustration) in java is an instance of the class 'java.lang.Class'. And whenever you define a class, the instance is created as java.lang.Class CallingStaticMethod = new java.lang.Class();
So the method is called on 'CallingStaticMethod ' and so null pointer exception will not occur.
Hope this helps.
Yes we can.
It will throw NullPointerException only if we are calling non static method with null object. If method is static it will run & if method is non static it will through an NPE...
To know more click here...