0

Simple Code I am putting, Complex one working :(

public class Kat {

public String name;
public Integer age;

 public static void accentureDetail()
 {
     System.debug('Anything to fix bug');
    }
      }

Execute

Kat d1 = new Kat();

Kat.accentureDetail(); // Not working

d1.accentureDetail(); // Not working
1
  • 3
    Kat.accentureDetail(); should compile and run. List the exact error message you are getting.
    – Keith C
    Commented Nov 15, 2017 at 0:15

1 Answer 1

3

Your method is static so you cannot reference it from a non static context or through an instance of that class.

Leaving your class as is, this line of code should work:

Kat.accentureDetail();

You can read more here:

A class static variable can’t be accessed through an instance of that class. If class MyClass has a static variable myStaticVariable, and myClassInstance is an instance of MyClass, myClassInstance.myStaticVariable is not a legal expression.

The same is true for instance methods. If myStaticMethod() is a static method, myClassInstance.myStaticMethod() is not legal. Instead, refer to those static identifiers using the class: MyClass.myStaticVariable and MyClass.myStaticMethod().

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .