为什么我不能使用静态变量的值?

时间:2016-10-07 19:30:43

标签: java variables static

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
       function my_conversion( $contact_form ) {
       $title = $contact_form->title;
       $submission = WPCF7_Submission::get_instance();

    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }

    if ( 'FORM NAME' == $title ) {

          $email = $posted_data["your-email"];
          $name  = $posted_data["your-name"];
          $last  = $posted_data["your-lastname"];
          $phone = $posted_data["tel-71"];
          $description = $posted_data["your-message"];


            $post_items[] = 'oid=00D4000000xxxxx';
            $post_items[] = 'first_name=' . $name;
            $post_items[] = 'last_name=' . $last;
            $post_items[] = 'company=';
            $post_items[] = 'email=' . $email;
            $post_items[] = 'phone=' . $phone;  
            $post_items[] = 'description=' . $description;
            $post_items[] = '00N4000000XXXX=' . "Shopping";
            $post_items[] = '00N4000000xxxxxx=' . "1";
            $post_items[] = 'external=1';
            $post_items[] = 'lead_source=Shopping';

        $post_string = implode ('&', $post_items);

        $ch = curl_init('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8' );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_exec($ch); // Post to Salesforce
        curl_close($ch); // close cURL resource
 }

为什么每次我尝试运行此方法字母成功运行但打印Null?我只想为所有方法使用一个变量,以便它们可以使用它的值。

1 个答案:

答案 0 :(得分:2)

当您在main方法中编写String symbol = args[0];时,您将声明一个 new 局部变量,该变量会隐藏(隐藏)名为symbol的静态字段。当您调用letter时,局部变量不再适用(因为它仅适用于main),因此打印静态字段的值,但该值为null,因为那样静态字段从未分配。

将该行更改为symbol = args[0];,以便将args[0]的值存储到静态字段中。