Java | Variables in Java Interfaces

Author - Webner

Interface:
In Java we have a concept called interfaces.A Java interface is same like a class, except a Java interface cannot contain an implementation of the methods, we have only signature of the method.It has static constants data member.

Can interfaces contain variables?
Yes, Interface can contain only public static final variables (by default all defined variables become public static final in Java interfaces since you cannot create objects of interfaces).

Because interface variables are static we can access them using interface name just like we access static variable using class.

Syntax for access interface variable:
InterfaceName.variable;

For eg:

Interface MyIfc {
    public int number = 20;
}

System.out.println(“number is & gt; & gt;”.MyIfc.number);

Use of interface variables:

By default every field in interface is public, static and final or in simple word a constant.
So they are a good way to be used as constants in Java.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *