{"id":14166,"date":"2020-04-29T06:13:36","date_gmt":"2020-04-29T10:13:36","guid":{"rendered":"https:\/\/blogs.mentor.com\/verificationhorizons\/?p=14166"},"modified":"2026-03-27T08:51:06","modified_gmt":"2026-03-27T12:51:06","slug":"systemverilog-static-methods","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2020\/04\/29\/systemverilog-static-methods\/","title":{"rendered":"SystemVerilog Static Methods"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In my <a href=\"https:\/\/blogs.mentor.com\/verificationhorizons\/blog\/2020\/04\/21\/systemverilog-classes-with-static-properties\/\" target=\"_blank\" rel=\"noopener\">last post<\/a>, you learned how to create a class with a static property. This variable acts like a global variable in that only one copy exists, no matter how many objects you create. This post shows how you can create methods that access those static properties.<\/p>\n<h3>Methods<\/h3>\n<p>Any class method can read and write these static properties, just like dynamic properties. However, if you declare the method as static, you can call the method <em>without <\/em>needing to construct an object. Here is the Thing class with a static method to print the count of Thing objects. You can call Thing::print_count(), even if no Thing objects were constructed.<\/p>\n<pre>class Thing;\n  int id;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Dynamic, unique to each object\n  <strong>static<\/strong> int count; \/\/ Static, associated with the class\u00a0\u00a0\u00a0\u00a0\u00a0 \n\n  <strong>static<\/strong> function void print_count();\n    $display(\u201cThing::count = %0d.\u201d, count);\n  endfunction\n  \/\/ The rest of the class\nendclass<\/pre>\n<p>Static methods can only access static properties. Remember, a static property acts like a global variable in that it is always around. Dynamic properties are stored in objects. The id property only exists in individual objects, so print_count() could not see it.<\/p>\n<p>Here is a simple error handler class that keeps track of how many error messages have printed and exits when you reach the maximum. Notice that there are no ErrorH handles or objects created. Copy this code and try on your simulator.<\/p>\n<pre>class ErrorH;\n  static int count=0, max_count=10;\n\n  static function void print(input string s);\n    $error(s);\n    if (++count &gt;= max_count) begin\n      $display(\"*** Error max %0d exceeded\", max_count);\n      $finish(0);\n    end\n  endfunction\n\n  static function void set_max_count(input int m);\n    max_count = m;\n  endfunction\nendclass\n\ninitial begin\n  ErrorH::set_max_count(2); \/\/ Limit the number of errors\n  ErrorH::print(\"one\");\n  ErrorH::print(\"two - should end sim\");\n  ErrorH::print(\"three - should not print\");\nend<\/pre>\n<h3>More tips<\/h3>\n<p>The uvm_config_db class is built on static methods, which is why you need to call with the following syntax.<\/p>\n<pre>uvm_config_db::set(...)\nuvm_config_db::get(...)<\/pre>\n<p>Without static methods, you would have to construct a DB object, and pass its handle around to every corner of the testbench. That defeats the whole goal of simplifying sharing of configuration information.<\/p>\n<p>The specialization is not shown. Look for more details in an upcoming blog post.<\/p>\n<p><em>Enjoy your verification journey!<br \/>\nChris Spear<\/em><\/p>\n<p>Keep learning at <a href=\"http:\/\/mentor.com\/training\" target=\"_blank\" rel=\"noopener\">mentor.com\/training<\/a><br \/>\nQuestions or ideas? <a href=\"https:\/\/verificationacademy.com\/ask-chris-spear\" target=\"_blank\" rel=\"noopener\">verificationacademy.com\/ask-chris-spear<\/a><br \/>\nView my recent webinar on <a href=\"https:\/\/verificationacademy.com\/sessions\/uvm-coding-guidelines-tips-and-tricks-you-probably-didnt-know\" target=\"_blank\" rel=\"noopener\">UVM Coding Guidelines<\/a> and the <a href=\"https:\/\/verificationacademy.com\/sessions\/uvm-coding-guidelines-tips-and-tricks-you-probably-didnt-know\/rte\/questions--answers\" target=\"_blank\" rel=\"noopener\">Questions and Answers<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In my last post, you learned how to create a class with a static property. This variable acts like&#8230;<\/p>\n","protected":false},"author":71586,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[7,1,982,10,983],"tags":[382,396,397,405,512,515,585,594,612,614,647,648,735,736,737,751,787,805,831],"industry":[],"product":[],"coauthors":[980],"class_list":["post-14166","post","type-post","status-publish","format-standard","hentry","category-learning-resources","category-news","category-systemverilog","category-tips-tricks","category-uvm","tag-class","tag-construct","tag-constructor","tag-create","tag-global","tag-handle","tag-member","tag-method","tag-object-oriented-programming","tag-oop","tag-properties","tag-property","tag-static","tag-static-method","tag-static-property","tag-systemverilog","tag-uvm","tag-uvm_config_db","tag-verilog"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/14166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/users\/71586"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/comments?post=14166"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/14166\/revisions"}],"predecessor-version":[{"id":18195,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/14166\/revisions\/18195"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media?parent=14166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/categories?post=14166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/tags?post=14166"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/industry?post=14166"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/product?post=14166"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/coauthors?post=14166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}