0% found this document useful (0 votes)
3 views

Class and Objects

Uploaded by

prabathkotti
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class and Objects

Uploaded by

prabathkotti
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

CLASS AND OBJECTS

Class:

A Class is a user defined datatype (complex-type) and acts as a blueprint for their
instances.

An Object is an instance of a class i.e. a class in action.

Class and Methods


//Class and Methods
public class Demo1{
public void printOutput(String stringToDisplay){
System.debug('Display text: ' + stringToDisplay);
}
}
Static vs Instance

Demo 2 – Static and Instance Variables & Methods

public class Demo2 {

String helloWorldString;
private static final String DEFAULT_STRING;

static{
DEFAULT_STRING = 'Hello World';
}

public Demo2(){
this(DEFAULT_STRING);
}
public Demo2(String stringToDisplay){
this.helloWorldString = stringToDisplay;
}

public static void printOutput(){


System.debug('Display text: ' + this.helloWorldString);
}
}

//Anonymous Window
Demo2 d21 = new Demo2();
d21.printOutput();

Demo2 d22 = new Demo2(‘Jigar’);


d22.printOutput();

Pass By Value Vs Pass By Reference In Apex


In Apex, all primitive data type arguments, such as Integer or String, are passed into
methods by value. This means that any changes to the arguments exist only within the
scope of the method. When the method returns, the changes to the arguments are lost.

Non-primitive data type arguments, such as sObjects, are also passed into methods by
value. This means that when the method returns, the passed-in argument still references
the same object as before the method call, and can’t be changed to point to another
object. However, the values of the object’s fields can be changed in the method.
Demo3 – Pass by Value Vs Reference
public class Demo3 {

public void mainValueMethod(){

String websiteUrl = 'www.nareshit.com';

System.debug('Before value call ' + websiteUrl);


passByValueCall(websiteUrl);
System.debug('After value call ' + websiteUrl);
}

private void passByValueCall(String websiteUrlValue){


//Pass by Value Call
websiteUrlValue = 'www.salesforce.com';
}
public void mainReferenceMethod(){

Account a = new Account();


a.Name = 'Test Account';
a.Website = 'www.google.com';

System.debug('Before reference call ' + a);


passByRefCall(a);
System.debug('After reference call ' + a);
}

private void passByRefCall(Account a){ //Pass


by Reference Call
a.Website = 'www.salesforce.com';
}
}

You might also like