Created
October 28, 2022 07:42
-
-
Save albarivas/77f05deacfab4d73b641c182f8d517ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creating a new date with some starting value | |
Date newDate = Date.newInstance(2022, 10, 27); | |
System.debug(newDate); | |
Date newDate2 = Date.newInstance(2023, 10, 27); | |
System.debug(newDate); | |
// Calling some non-static methods of the date instance | |
Integer year = newDate.year(); | |
System.debug('year :' + year); | |
Integer year2 = newDate2.year(); | |
System.debug('year :' + year2); | |
// Calling static methods of the date class | |
Boolean isLeapYear = Date.isLeapYear(2022); | |
System.debug('isLeapYear :' + isLeapYear); | |
// Calling a custom class instance methods | |
Integer result = MyCalculator.sum(1,6); | |
System.debug('result :' + result); | |
MyCounter birdCounter = new MyCounter(); | |
birdCounter.incrementCounter(); | |
MyCounter dogCounter = new MyCounter(); | |
dogCounter.decrementCounter(); | |
birdCounter.incrementCounter(); | |
// Instantiate a custom class after adding a constructor with params | |
MyCounter catCounter = new MyCounter('Cat'); | |
catCounter.incrementCounter(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class MyCalculator { | |
// Static methods are used to perform instant calculations, | |
// method starts, runs, and returns something, but nothing is stored in memory! | |
public static Integer sum (Integer a, Integer b) { | |
return a + b; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class MyCounter { | |
private Integer counter = 0; | |
private String counterName = 'Default counter name'; | |
/* A default constructor with no parameters exists for each class, | |
if you need to define a constructor with parameters, do it this way: | |
public MyCounter(String counterNameParam) { | |
// Do whatever | |
this.counterName = counterNameParam; | |
}*/ | |
// When you create a instance of a class, its state is stored | |
// in its instance variables (counter in this case) --> instance methods can see and modify those vars | |
public void incrementCounter() { | |
this.counter++; | |
System.debug(this.counterName + ' counter value is: ' + this.counter); | |
} | |
public void decrementCounter() { | |
this.counter--; | |
System.debug(this.counterName + ' counter value is: ' + this.counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment