-1

I am starting to learn Java and have a nooby question. I have a set of instances in my class block with 2 methods, 1 main static, and 1 void

public class CurrencyConverter {
    int rupee = 63;
    int dirham = 3;
    int real = 3;
    int chilean_peso = 595;
    int mexican_peso = 18;
    int _yen = 107;
    int $austrailian = 2;
    int dollar = 0;
    int Rupee = 63;

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here
        CurrencyConverter cc = new CurrencyConverter();
        cc.printCurrencies();
    }

    void printCurrencies() {
        System.out.println("rupee: " + rupee);
        System.out.println("dirham: " + dirham);
        System.out.println("real: " + real);
        System.out.println("chilean_peso: " + chilean_peso);
        System.out.println("mexican_peso: " + mexican_peso);
        System.out.println("yen: " + _yen);
        System.out.println("australian: " + $austrailian);
        System.out.println("dollar: " + dollar);
        System.out.println("Rupee: " + Rupee);                
    }       
}

Now my question, why do I need to instantiate my CurrencyConverter class in order to call printCurrencies()? Can't you normally just call methods anyway? I am in the same class block?

I tried changing the access modifier of printCurrencies() to static but then my local variables aren't static

Why do I NEED to instantiate?

Mat
  • 202,337
  • 40
  • 393
  • 406
  • `printCurrencies` is an instance method - it acts on an *instance* of the class, so you need to have created an instance first. Note that `static` isn't an *access* modifier like public/private - it's a modifier that says whether the method/field/etc is related to an instance of the type or a type itself. Also note that your variables such as `rupee` etc are fields, not local variables. The only *local* variable in your code is `cc`. I'd suggest reading up more about what `static` means in a good Java book or tutorial. – Jon Skeet Oct 06 '18 at 06:52
  • Check out this thread on the difference between static and instance methods. https://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – Rans Oct 06 '18 at 06:52
  • I would kindly suggest you to learn oops concepts before asking these questions.I am sure that next time you won't ask..It happens to everyone buddy....Don't worry – Ganesh Chowdhary Sadanala Oct 06 '18 at 06:59

2 Answers2

1

Non-static fields are associated with an instance. You have one copy of these fields per instance.

public class CurrencyConverter {
    int rupee = 63; // non static instance field
    int dirham = 3; // non static instance field
    // etc.

why do I need to instantiate my CurrencyConverter class in order to call printCurrencies()?

Without an instance, you have zero copies thus nothing to print.

Can't you normally just call methods anyway?

If you make the method static and remove all references to instance fields, then yes, you could. This runs just fine, but it no longer does anything useful.

public static void main(String[] args) {
    printCurrencies();
}

static void printCurrencies() {
}  

I am in the same class block?

Not sure what you mean, but there is only one class and everything is in it.

int rupee = 63;
int Rupee = 63;

Don't do this unless you like confusion. You should make the different purpose of each field clear in the name.

I tried changing the access modifier of printCurrencies() to static but then my local variables aren't static. Why do I NEED to instantiate?

A copy of the non-static fields doesn't exist until you explicitly create them.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

think of a class as a pattern or a recipe for an object. When instantiating you create an object with this specific pattern. Now there is an object with values you can access. The pattern(so the class) has only a description of the field that can hold specific values. Therefore there is no value you can access.

And now statics: Static fields are fields which are created at the beginning of the runtime. Therefore you can access the value anytime without creating an object because they don't belong to an object but to a particular class.

Your solution to get rid of the instantiating is making all members of that class static.

(remember: every const member is static)

s0me1
  • 109
  • 7