How to instantiate an object in java?

后端 未结 7 1024
不思量自难忘°
不思量自难忘° 2020-12-01 13:14

I\'m new in programming and I would like to know where did I go wrong in instantiating an object. Below is the code:

public class Testing{
    private int Sa         


        
相关标签:
7条回答
  • 2020-12-01 13:56

    Sample is not a class, it is just a method. You cannot create instances of it. You only run it -

    int sample = Sample(3);
    

    If you wish for sample to be a class, define it as a class.

    In your case, the method is not static is so you cannot directly access it from the Static method Main. Make it static so you could access it. Or just create a new instance of Testing and use it -

    Testing testing = new Testing();
    int sample = testing.Sample(3);
    
    0 讨论(0)
提交回复
热议问题