Static to non-static

List com.crio.warmup.stock.portfolio.PortfolioManager.calculateAnnualizedReturn(List portfolioTrades, LocalDate endDate) throws JsonMappingException, JsonProcessingException

Cannot make a static reference to the non-static method calculateAnnualizedReturn(List, LocalDate) from the type PortfolioManagerJava(603979977)

I am getting this error while calling the function whose functionality I wrote in PortfolioImpl.java.

Read this and apply it here

But how to call a non-static method from a static function?

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

I’m closing this topic due to no response. If your query is resolved you can mark any reply as a solution. If you still have this query, please create a new topic and paste the link of this topic in the description.

Hey, did you solve this error?

Understood what static and non static methods are but no idea how to call getStockQuote() into CalculateAnnualizedReturns() as we don’t have an object here.

yeah,There is an object already made in test file. My problem I was hardcoding .

But which object should I use to call the method in calc()?

If you are asking how to use getStockQuote() then you don’t need any object for that just use it inside CalculateAnnualizedReturns() and return List of TiingoCandle instead of directly using TiingoCandle inside CalculateAnnualizedReturns() function

I tried doing that but as you can see, Calc() is static and getStock() isn’t and we can call getStock() inside a static method without using an object. That’s the error that I’m getting.

calc() should be public only instead of static

1 Like

SO should I just remove the ‘static’ keyword that’s written in it’s definition? That doesn’t seem the right way to me.
However, I tried doing that and it is passing all the cases but I am not sure if it will still work for the upcoming modules. :thinking:
Anyway, thank you.

If you are defining calc() function inside PortfolioManagerImpl then there is no need of static.

Make calculateAnnualizedReturn and getStockQuote public. Now, call getStockQuote function inside calculateAnnualizedReturn. Get your stock quotes and move forward.
If you need more clarification. Will give you!

Why these are public method instead of public static?
Ans. See each user will have different profile data(specific to user). Now when we use static key word it will make it as class level method. Which means each object can share these methods. Do we actually need these type of functionality. A big NO, we want the calculateAnnualizedReturn and getStockQuote work individually for individual user. Thus if you check test file for these methods they are making an object of PortfolioManagerImp and then calling these methods. If we make these methods as static then the calling of function will be different and we can directly call these methods by just using class name. Which will not be user specific.

2 Likes

AWESOME. Thanks :slight_smile:

1 Like