简体   繁体   中英

Java can't access class methods from class instance

I've defined Class A which has number of methods. Then I have this other class ie a managed beanfor JSF. Within the bean I create an instance of Class A, but then I can't invoke any of the methods in class A. All fields are public and methods scope are public too.

I considered this could be because of the bean nature (Although it shouldnt' be) so I created another class Tester.java and created the instance and that went ok. But again when I try to invoke the methods, nothing shows in suggestion list in Netbeans. What is going on? Thanks,

Edit: The code:

public class Reservation {
.... //setters & getters

  public List<DateTime> getDateRange(DateTime start, DateTime end) {
  ......//body of method
  }

   public TreeMap<DateTime, Integer> getDatesTreeMap(){
   //body of method
   }

   public boolean checkRange() {
   ... body of method
   }

   }//end of class - no errors

and then this is how class instantiated:

Reservation booking = new Reservation();
booking. ????? this is where the suggestions don't come up 

Thanks

A guess (since you still are not showing enough code to know for sure, but...)

You are likely trying to call methods out in the class and outside of a method or constructor block. In other words, this code:

Reservation booking = new Reservation();
booking. ????? this is where the suggestions don't come up 

is likely called in the declarations section of your class, but not inside of a method block, a constructor block, or other similar construct. Only variable declarations and their related initialization code may be called here, but other statements such as calling methods on variables cannot.

The solution: call the code where it belongs, in a method or constructor block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM